PHP Course Note 7

Source: Internet
Author: User

Lesson 141 How PHP Abstracts a class

1. The class name should be descriptive, plus a two-level name. Class

2. If the class name has more than one word, the first letter of each word is capitalized and the hump is named.

Lesson 142 member properties and member methods in PHP classes

1. The member attribute must be preceded by a modifier

var Public private protected

Lesson 143 php instantiation Objects

$tom = new Person (); Constructs are called when instantiated
$tom->name = "Tom";

Lesson 144 PHP drawing understand the form of objects in memory

Class 145 Special Object reference "$this"

$this used in a method to access a variable in a class, if a var $name is defined, it needs to be written as $this->name in the method.

Lesson 146 php construction method

1.__construct () Construction method

Is the first method that is automatically called after object creation is complete. Special

2. Method names are special and can be the same as class names.

3. Assign an initial value to the members of the object.

4. If a construct is present with the same name, then only the CONSTURCT is called

Class 147 php destructor method

1.__destruct destructor method

The destructor method is executed before the object is freed. No parameters exist.

There is garbage collection in PHP, so destructors are not used for garbage collection. It is used to release resources, or to recycle resources.

For example, after the database is used, the database is closed in the destruction method.

Lesson 148 Review Front object-oriented knowledge

1. When two methods use the same variable, the variable is generated as a member property. Otherwise, the direct local variable is OK.

<?php

Class person{

var $name;
var $age;
var $sex;

function __construct ($name, $age, $sex) {
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}

function say () {
echo "My name is". $this->name. ". My age is ". $this->age." and I a ". $this->sex;

}
}

$sce = new Person ("SCE", "All", "man");
echo $sce->say ();
?>

Lessons 149 PHP Object-oriented encapsulation 1

1. Encapsulation

The private method in the object is not available externally, but other members can use it.

The encapsulation method is for internal service, for other unpackaged methods of service. So when it comes to demand, just look at the unpackaged function.

Hours of Class PHP object-oriented encapsulation 2

1. Encapsulation of member properties

Assigning a value to a property is dangerous, so you can't change it arbitrarily. needs to be encapsulated.

Encapsulation is a protection for read properties.

The encapsulated property to invoke can be called first by means of a method.

lesson 151 PHP Object-Oriented encapsulation Magic Method 1

__get ()

1. Automatic invocation. Automatically called when accessing private members directly.

__set ()

1. Automatic invocation is used when setting up private members directly.

Class 2 Magic method for object-oriented encapsulation of PHP

__isset

When you use Isset () to determine whether a private property exists, the __isset is called automatically. The parameter is the property name.

__unset

You can delete a private property.

PHP Course Note 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.