PHP construction method, destructor method and this keyword in detail _php tutorial

Source: Internet
Author: User
I. What is a construction method
The constructor method is a special method of the class, and its main function is to complete the initialization of the new object.
Characteristics:
1. No return value.
2. When creating a new object, the system automatically calls the class's construction method to complete the initialization of the new diagonal.
Grammar:
PHP5: Modifier function __construct ()

{
Code

}
PHP4: Modifier function class name ()

{
Code

}
Attention:
1. The php5 supports both, and the first option is preferred if both methods of construction exist at the same time.
2. In a class, there is a default constructor with null parameters, and once a constructor is customized, the default constructor method is overridden.

So a class has and only one construction method.
3. A class can have only one method of construction. (cannot be overloaded)
4. Constructor method The default access modifier is public.
Two. this keyword
This represents the current object. Can be understood as: who calls it, it represents who.
Precautions:
This is not used in class definitions and can only be used in methods defined by the class.
Three. Example
Copy the Code code as follows:
Header ("Conter-type:text/html;charset=utf-8");
Class Person
{
Public $name; Member variables
Public $age;

function __construct ()
//{
echo "method of constructing without parameters";

//}
function __construct ($name, $age)
{
$this, name = $name;
$this, age = $age;
echo "Construction method with parameters".
";
}
Member Methods
Function View ()
{
The reference to this.
echo "Name:". $this->name. ", Age:". $this->age;

}
}
New Object
$p = new Person ();
$p 2 = new Person ("John Doe", 13);
$p 2->view ();
?>

The results are as follows:
Method of constructing with parameters
Copy the Code code as follows:
Name: John Doe, Age:

Four: The method of Destruction:
The destructor method is a new concept introduced by PHP5. Main role: Release resources (such as: Free database links, image resources ...).
Grammar:
function __destruct () {}
Characteristics:

1. The destructor method has no return value.

2. The main role is to release resources. Not destroying the object itself.
3. The system automatically calls the destructor of the class before destroying the object.

4. A class has a maximum of one destructor method.

V: Example:
Copy the Code code 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;
$this->age = $age;

}
destructor method
function __destruct ()
{
echo "Name:". $this->name. ", age". $this->age. " --Destroy
";
}

}

$p 1= New Person ("small One", 18);
$p 2= New Person ("small two", 17);
?>

Results:
Name: Waiter, age 17--> destroyed
Name: Little one, age 18--> destroyed

Analysis Conclusion:
1. The destructor method is called automatically.

2. The order in which the destructor is called is destroyed after the object was created first.

3. When an object is not referenced and is recognized as garbage by the garbage collection mechanism, the destructor method is called.

http://www.bkjia.com/PHPjc/824884.html www.bkjia.com true http://www.bkjia.com/PHPjc/824884.html techarticle I. What is the constructor method is a special method of the class, its main function is to complete the initialization of the new object. Features: 1. No return value. 2. When creating a new object ...

  • 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.