php constructor and destructor

WBOY
Release: 2016-07-25 08:53:53
Original
782 people have browsed it
In PHP programming, there is a function called the constructor in object-oriented programming, which is a method that is automatically called when an object is created to complete class initialization.

Because this function will be automatically executed as soon as the PHP class is loaded, and generally the initialization work is placed in this function.

1. The constructor, like other functions, can pass parameters and set default parameter values.

2. The constructor can call properties and methods.

3. The constructor can be explicitly called by other methods.

In previous PHP versions, the constructor used the same name as the class name to initialize the object. However, it was later discovered that if you want to change the class name, you must also change its construction method, so in PHP5, it is used __construct( ) function to implement. __construct() is preceded by two underscores, do not think it is one underscore.

Corresponding to the constructor is the destructor. It is executed when an object becomes garbage or when the object is explicitly destroyed.

__destruct() destructor is executed when the garbage object is recycled.

1. Do not call the destructor of an object in the program.
2. The destructor cannot have parameters.
3. Usually php will automatically perform garbage collection after the program ends.

Because when creating a class, sometimes there will be multiple instances, so how is the constructor called?

PHP's constructor call searches upwards from itself and executes the most recent one to make the call.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!