Home >Backend Development >PHP Tutorial >Detailed introduction to PHP's construction method, destructor method and this keyword_PHP Tutorial

Detailed introduction to PHP's construction method, destructor method and this keyword_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:011100browse

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
                                                                                                                                                                       ;

                                                                                                                                                                                                                                                                            through

} <:> Note:
1. PHP5 supports both. If the two constructed methods exist at the same time, choose the first type.

2. In a class Once a constructor with empty parameters is customized, the default constructor will be overridden.


So a class has one and only one constructor.

3. A class can only have one constructor. (Cannot be overloaded)

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:

this is not used in class definitions, but can only be used in class definition methods Used in.

3. Example



Copy code

The code is as follows:

header( "Conter-Type:text/html;charset=utf-8"); class Person { public $name; //Member variable
public $age;

// function __construct()
         //{
                                                                                                                                                                                                                 >                   { { ".$this ->age;

}
}
} //new a new object
//$p = new Person();
$p2 = new Person("李思",13);
$p2 ->view();
?>




The result is as follows:
Constructor method with parameters



Copy code

The code is as follows:


Name: Li Si, Age: 13


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:

Copy code The code is as follows:

header("Conter -Type:text/html;charset=utf-8");

class Person
{
public $name;
public $age;
//Construction method
function __construct($name,$age)
{
$this ->name = $name;
age = $age;

}
//Destruction method
function __destruct()
{

";
                                                                                     二",17);
?>




Result: Name: Xiaoer, age 17-->Destroy

Name: Xiaoyi, age 18-->Destroy

Analysis conclusion:

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

truehttp: //www.bkjia.com/PHPjc/824884.htmlTechArticle1. What is a constructor method? A constructor method is a special method of a class. Its main function is to complete the New object initialization. Features: 1. No return value. 2. When creating a new object...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn