Home >Backend Development >PHP Tutorial >Example explanation of PHP 5.0 constructor_PHP tutorial
Everyone is right If you declare a function in a class and name it __construct, this function will be treated as a constructor and executed when an object instance is created. To be clear, __ is two underscores. Just like any other function, PHP 5.0 constructors 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. One class (subclass/derived class) can inherit the functionality of another class (parent class/base class). The derived class will contain all the properties and methods of the base class, and other properties and methods can be added to the derived class. You can also override base class methods and properties. As shown previously, you can extend a class using the extends keyword.
You may be wondering how PHP 5.0 constructors are inherited. When they are inherited along with other methods, they are not executed when the object is created.
If you need this feature, you need to use the :: operator. It allows you to point to a namespace. parent points to the parent class namespace. You can use parent::__construct to call the parent class's constructor. .
Some object-oriented languages name the constructor after the class. The same was true for previous versions of PHP, and this method is still valid. That is: if you name a class Animal and create a method named Animal in it, then this method is the constructor. If a class With 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 PHP 5.0 constructors to have a unique name, regardless of the name of the class it is in. This way you don't need to change the name of the constructor when you change the name of the class.
You may give constructors the same access as other class methods in PHP. Access methods will affect the ability to instantiate objects from a certain range. This allows the implementation of some fixed design patterns, such as the Singleton pattern.
Destructor, the opposite of constructor. PHP calls them to destroy an object from memory. By default, PHP only releases the memory occupied by object properties and destroys object-related resources. Destructors allow you to execute arbitrary code to clear memory after using an object.
When PHP decides that your script is no longer associated with an object, the destructor will be called. Within a function's namespace, this happens when the function returns. For global variables, this happens on At the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable pointing to the object. Usually assign the variable to NULL or call unset.
In the following example, the number of objects instantiated from the class is counted. The Counter class starts to increment in the PHP 5.0 constructor and decrements in the destructor.
<ol class="dp-xml"> <li class="alt"><span><span>class Counter </span></span></li> <li class=""><span>{ </span></li> <li class="alt"> <span> private static $</span><span class="attribute"><font color="#ff0000">count</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; </span> </li> <li class=""><span> </span></li> <li class="alt"><span> function __construct() </span></li> <li class=""><span> { </span></li> <li class="alt"><span>self::$count++; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> function __destruct() </span></li> <li class="alt"><span> { </span></li> <li class=""><span>self::$count--; </span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> function getCount() </span></li> <li class=""><span> { </span></li> <li class="alt"><span>return self::$count; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>//建立第一个实例 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">c</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Counter(); </span> </li> <li class="alt"><span> </span></li> <li class=""><span>//输出1 </span></li> <li class="alt"><span>print($c->getCount() . "n"); </span></li> <li class=""><span> </span></li> <li class="alt"><span>//建立第二个实例 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">c2</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Counter(); </span> </li> <li class="alt"><span> </span></li> <li class=""><span>//输出2 </span></li> <li class="alt"><span>print($c->getCount() . "n"); </span></li> <li class=""><span> </span></li> <li class="alt"><span>//销毁实例 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">c2</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">NULL</font></span><span>; </span> </li> <li class="alt"><span> </span></li> <li class=""><span>//输出1 </span></li> <li class="alt"><span>print($c->getCount() . "n"); </span></li> <li class=""><span>?> </span></li> </ol>
Once you define a class, you can use new to create an instance of the class. The definition of the class is the design diagram, and the instance is A component placed on an assembly line. New requires the name of a class and returns an instance of that class. If the PHP 5.0 constructor requires parameters, you should enter the parameters after new.