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.