PHP constructor, destructor, and this keyword details _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP constructor, destructor, and this keyword. I. what is constructor is a special method of the class, and its main function is to complete the initialization of the new object. features: 1. no return value. 2. when creating a new object. what is constructor?
Constructor is a special class method. its main function is to complete initialization of new objects.
Features:
1. no return value.
2. when creating a new object, the system automatically calls the constructor of this class to initialize the new diagonal.
Syntax:
Php5: modifier function _ construct ()

{
// Code

}
Php4: modifier function class name ()

{
// Code

}
Note:
1. both methods are supported in php5. if both constructor methods exist, the first one is preferred.
2. a class contains a constructor without any parameter. Once a constructor is defined, the default constructor is overwritten.

Therefore, a class has only one constructor.
3. a class can have only one constructor. (reload is not allowed)
4. the default access modifier of the constructor is public.
II. this keyword
This represents the current object. it can be understood as: who calls it, it represents who.
Note:
This is not used in class definition and can only be used in class definition methods.
III. instances

The code is as follows:


Header ("Conter-Type: text/html; charset = utf-8 ");
Class Person
{
Public $ name; // member variable
Public $ age;

// Function _ construct ()
//{
// Echo "constructor without parameters ";

//}
Function _ construct ($ name, $ age)
{
$ This-> name = $ name;
$ This-> age = $ age;
Echo "construction method with parameters "."
";
}
// Member method
Function view ()
{
// Reference of this.
Echo "name:". $ this-> name. ", age:". $ this-> age;

}
}
// A new object
// $ P = new Person ();
$ P2 = new Person ("Li Si", 13 );
$ P2-> view ();
?>

The result is as follows:
Construction method with parameters

The code is as follows:


Name: Li Si, age: 13

IV. destructor:
The structure analysis method is a new concept introduced by PHP5. it mainly plays a role in releasing resources (for example, releasing database links and image resources ...).
Syntax:
Function _ destruct (){}
Features:

1. the destructor has no return value.

2. the main function is to release resources, not to destroy the object itself.
3. the system automatically calls the destructor of this class before the object is destroyed.

4. a class can have at most one destructor.

V. example:

The code is as follows:


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

Class Person
{
Public $ name;
Public $ age;
// Constructor
Function _ construct ($ name, $ age)
{
$ This-> name = $ name;
$ This-> age = $ age;

}
// Destructor
Function _ destruct ()
{
Echo "name:". $ this-> name. ", age". $ this-> age. "--> destroy
";
}

}

$ P1 = new Person ("Xiaoyi", 18 );
$ P2 = new Person ("", 17 );
?>

Result:
Name: Xiao'er, age 17 --> destroy
Name: Xiaoyi, age 18 --> destroy

Analysis conclusion:
1. the destructor is automatically called.

2. the sequence of method calls is to create an object first and then destroy it.

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

The struct constructor 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.