Home >Backend Development >PHP Tutorial >Section 4 - Constructor and Destructor - Classes and Objects in PHP5 [4_PHP Tutorial
Section 4 - Constructors and Destructors
If you declare a function in a class, named __construct, this function will be treated as a constructor and executed when an object instance is created. Clearly Say, __ is two underscores. Just like any other function, a constructor may have parameters or default values. You can define a class to create an object and put all its properties in a statement.
You can also define a function called __destruct, which PHP will call before the object is destroyed. It is called a destructor.
Inheritance is a powerful feature of classes. A class (subclass/derived class ) can inherit the functions of another class (parent class/base class). The derived class will contain all the properties and methods of the base class, and can add other properties and methods to the derived class. You can also override the base class's Methods and properties. As shown in 3.1.2, you can extend a class using the extends keyword.
You may wonder how constructors are inherited. When they are inherited along with other methods, they It will not be executed when creating an object.
If you need this function, you need to use the :: operator mentioned in Chapter 2. It allows you to point to a namespace. parent points to the parent class namespace, you can use parent::__construct to call the constructor of the parent class.
Some object-oriented languages name the constructor after the class. The same was true for previous versions of PHP, and this method still works. That is: if you put a If a class is named Animal and a method named Animal is created in it, this method is the constructor. If a class has both a __construt constructor and a function with the same name as the class, PHP will treat __construct as a constructor. This allows classes written in previous PHP versions to still be used. But new scripts (PHP5) should use __construct.
This new way of declaring constructors in PHP allows the constructor to have a unique name , no matter what the name of the class it is in is. In this way, when you change the name of the class, you do not need to change the name of the constructor.
You may give the constructor the same access method as other class methods in PHP. Access methods will affect the ability to instantiate objects from a certain scope. This allows the implementation of some fixed design patterns, such as the Singleton pattern.
Destructors, as opposed to constructors. PHP calls them to remove an object from memory Destruction. By default, PHP only releases the memory occupied by the object's properties and destroys the resources associated with the object. The destructor allows you to execute arbitrary code to clear the memory after using an object.
When PHP decides that your script is no longer related to The destructor will be called when an object is associated with it. Within a function's namespace, this happens when the function returns. For global variables, this happens at the end of the script. If you want to explicitly destroy an object, you Any other value can be assigned to the variable pointing to the object. Usually the variable is assigned to NULL or unset is called.
In the following example, the number of objects instantiated from the class is counted. The Counter class starts incrementing the value from the constructor , decrease the value in the destructor.