PHP is still relatively commonly used, so I studied the PHP constructor and shared it with you here. I hope it will be useful to everyone. If you declare a function in a class, named __construct, the function will be treated as a constructor and executed when creating an object instance. To be clear, __ is two underscores. Just like any other function Likewise, 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 class called __destruct Function, PHP will call this function before the object is destroyed. It is called the destructor. Inheritance is a powerful feature of classes. A class (subclass/derived class) can inherit another class (parent class/base class) Function. The derived class will contain all the properties and methods of the base class, and can add other properties and methods in the derived class. You can also override the methods and properties of the base class. As shown in 3.1.2, you You can use the extends keyword to inherit a class.
You may wonder how constructors are inherited. When they are inherited along with other methods, they will not be executed when the object is created. If you 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, and you can use parent::__construct to call the parent class's constructor.
PHP’s new method of declaring constructors allows the constructor 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 the constructor in PHP the same access method as other class methods. The access method 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, are called by PHP to destroy an object from memory. By default, PHP only releases the memory occupied by the object's properties and destroys the resources associated with the object. Destructors allow you to execute arbitrary code to clear memory after using an object.
When PHP decides that your script no longer When 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 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 is derived from The PHP constructor begins to increment and the destructor decrements. Once you define a class, you can use new to create an instance of the class. The definition of the class is the design drawing, and the instance is the component placed on the assembly line. New Requires the name of the class and returns an instance of the class. If the PHP constructor requires parameters, you should enter the parameters after new.
<ol class="dp-xml"> <li class="alt"><span><span> </span></span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>phpclassCounter</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>privatestatic$</SPAN><SPAN class=attribute><FONT color=#ff0000>count</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>function__construct() </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>self::$count++; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>function__destruct() </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>self::$count--; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>functiongetCount() </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>returnself::$count; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>//建立第一个实例 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>c</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>newCounter</FONT></SPAN><SPAN>();//输出1 </SPAN></SPAN><LI class=""><SPAN>print($c-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>getCount()."</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>n"); </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">newCounter</font></span><span>(); </span> </li> <li class="alt"><span>//输出2 </span></li> <li class=""> <span>print($c-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getCount()."</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>n"); </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>//输出1 </span></li> <li class=""> <span>print($c-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getCount()."</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>n");</span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
When you create a new instance, memory will be prepared for storage All properties. Each instance has its own unique set of properties. But methods are shared by all instances of the class.