PHP constructor-php Tutorial

Source: Internet
Author: User
PHP constructor this php Tutorial mainly describes PHP constructor. There is a special function in object-oriented programming. This function is called a constructor. It is a method automatically called when an object is created and used to complete class initialization. As long as the PHP class is loaded, this function is automatically executed, and initialization is generally stored in this function. 1. Constructor "> <LINKhref =" http: // www. php10

PHP constructor
This php Tutorial mainly describes PHP constructor.

There is a special function in object-oriented programming. This function is called a constructor. It is a method automatically called when an object is created and used to complete class initialization. As long as the PHP class is loaded, this function is automatically executed, and initialization is generally stored in this function.
1. Like other functions, constructor can pass parameters and set the default values of parameters.
2. Constructor can call attributes and call methods.
3. Constructors can be explicitly called by other methods.

In the previous PHP version, the constructor uses the same name as the class name to initialize the object. However, if you want to change the class name, you must change its constructor, therefore, PHP5 is implemented using the _ construct () function. _ construct () consists of two underscores.

The Destructor corresponds to the constructor. It is executed when an object becomes junk or when the object is explicitly destroyed. The _ destruct () destructor is executed when the garbage object is recycled.
1. Do not call the destructor of an object in a program.
2. The destructor cannot contain parameters.
3. After the program ends, PHP automatically executes garbage collection.
Here is a question. Because we create a class and sometimes there are multiple instances, how can we call the constructor. Php constructor calls the most recent php constructor from its own lookup and execution.

Let's add more about this constructor later. The basic knowledge of this constructor is just like this. I believe that I am overwhelmed by the inexplicable functions such as _ construct. As for its practical application, let's talk about it now. We will not give an example today. It's hard to stop working today.

 

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.