Php constructor and Destructor

Source: Internet
Author: User
Php constructor and Destructor
In php programming, an object-oriented function is called a constructor. it is a method automatically called when an object is created 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. php automatically executes garbage collection after the program ends.

Because sometimes multiple instances are created for a class, how does the constructor call it.

Php constructor calls the most recent php constructor from its own lookup and execution.

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.