Home > Article > Backend Development > Detailed introduction to PHP's construction method, destructor method and this keyword_PHP Tutorial
1. What is a constructor method
The constructor method is a special method of a class. Its main function is to complete the initialization of new objects.
Features:
1. There is no return value.
2. When creating a new object, the system will automatically call the constructor method of the class to complete the initialization of the new object.
Syntax:
php5: Modifier function __construct()
through
;
} <:> Note:
1. PHP5 supports both. If the two constructed methods exist at the same time, choose the first type.
So a class has one and only one constructor.
4. The default access modifier of the constructor is public.
2. this keyword
This represents the current object. It can be understood as: whoever calls it, it represents.
Note:
3. Example
Copy code
The code is as follows:
Four: Destruction method:
Destruction method is a new concept introduced by PHP5. Main function: releasing resources (for example: releasing database links, image resources...).
Syntax:
function __destruct(){}
Features:
1. The destructor method has no return value.
2. The main function is to release resources. It is not to destroy the object itself.
3. Before destroying the object, the system automatically calls the destructor method of the class.
4. A class can have at most one destructor method.
Five: Example:
Name: Xiaoyi, age 18-->Destroy
1. The destructor method will be called automatically.
2. The order of calling the destructor method is that the object is created first and then destroyed.
3. When an object has no reference and is confirmed as garbage by the garbage collection mechanism, the destructor method is called.
http://www.bkjia.com/PHPjc/824884.html
www.bkjia.com