PHP construction method, destructor and this keyword detail _php tips

Source: Internet
Author: User

I. What is a construction method
The construction method is a special method of the class, whose main function is to complete initialization of the new object.
Characteristics:
1. No return value.
2. When a new object is created, the system automatically invokes the constructor method of the class to complete the initialization of the new diagonal.
Grammar:
PHP5: Modifier function __construct ()

{
Code

}
PHP4: Modifier function class name ()

{
Code

}
Attention:
1. PHP5 support both, if the two construction methods exist at the same time, the first choice of priority.
2. A class has a default construction method with no parameters, and once a constructor is customized, the default construction method is overwritten.

So one class has and only one construction method.
3. A class can have only one construction method. (Cannot overload)
4. The default access modifier for the construction method is public.
Two. this keyword
This represents the current object. It can be understood as: who calls it, it represents who.
Precautions:
This is not used in the class definition, only in methods defined by the class.
Three. Examples

Copy Code code as follows:

<?php
Header ("Conter-type:text/html;charset=utf-8");
Class Person
{
Public $name; Member variable
Public $age;

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

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

}
}
A new object.
$p = new Person ();
$p 2 = new Person ("Dick", 13);
$p 2->view ();
?>

The results are as follows:
A construction method with parameters

Copy Code code as follows:

<span style= "WIDOWS:2; Text-transform:none; text-indent:0px; font:14px Microsoft James Black; White-space:normal; Orphans:2; Letter-spacing:normal; COLOR: #ff00ff; word-spacing:0px "color=" #ff00ff "> Name: Dick, age:13</span>

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

1. There is no return value for the destructor.

2. The primary role is to release resources. The object itself is not destroyed.
3. The system automatically invokes the destructor of this class before destroying the object.

4. A class has only one destructor at most.

V: Examples:

Copy Code code as follows:

<?php
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 <br/> ";
}

}

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

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

Analysis Conclusion:
1. The destructor is invoked automatically.

2. The order of the destructor calls is the object that was first created and then destroyed.

    3. When an object is not referenced and is identified as garbage by the garbage collection mechanism, the destructor is invoked.

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.