Home  >  Article  >  Backend Development  >  Quickly master the principles of using PHP constructors_PHP tutorial

Quickly master the principles of using PHP constructors_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:27:59819browse

Constructor is the basic syntax in PHP. What is a constructor? Let’s take a look at the PHP constructor here. 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. You may want to know how the constructor is inherited . When they are inherited along with other methods, they will not be executed when the object is created.

Some object-oriented languages ​​name the constructor after the class The same is 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 If you have 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.PHP's new method of declaring a constructor allows the constructor to have a unique name, no matter what the name of the class it is in. In this way, you do not need to change the name of the constructor when you change the name of the class. .

PHP constructors are accessed in the same way as other class methods. The access method 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, In contrast to constructors, PHP calls them 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 after using an object. Clear memory. 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 at the end of the script. time. 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 calculation is from the class The number of instantiated objects. The Counter class is incremented from the constructor and decremented in the destructor. Once you define a class, you can use new to create an instance of this class. The definition of a class is the design diagram, and the instance It is a component placed on the assembly line. New requires the name of the class and returns an instance of the class. If the constructor requires parameters, you should enter the parameters after new.

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>classCounter  </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>();  </SPAN></SPAN><LI class=""><SPAN>//输出1  </SPAN><LI class=alt><SPAN>print($c-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></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=""><span>//建立第二个实例  </span></li>
<li class="alt">
<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=""><span>//输出2  </span></li>
<li class="alt">
<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=""><span>//销毁实例  </span></li>
<li class="alt">
<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=""><span>//输出1  </span></li>
<li class="alt">
<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="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

When you create a new For an instance, memory will be prepared to store all properties. Each instance has its own unique set of properties. But methods are shared by all instances of the class.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446477.htmlTechArticleConstructor is the basic syntax in PHP. What is a constructor? Let’s take a look at the PHP constructor here. If you declare a function in a class, name it __constr...
Statement:
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