Home  >  Article  >  Backend Development  >  How to correctly understand PHP destructor_PHP tutorial

How to correctly understand PHP destructor_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:33:161005browse

First time learning

If you declare a function in a class and name it __construct, this function will be regarded as a constructor and create an object instance is executed. To be clear, __ 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 one statement ( statement).

You can also define a function called __destruct, which PHP will call before the object is destroyed. It is called the PHP destructor.

Inheritance is a class A powerful feature. 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 be added to the derived class Other properties and methods. You can also override methods and properties of the base class. As shown in 3.1.2, you can inherit a class using the extends keyword.

You may want to know what the constructor is How they are inherited. When they are inherited along with other methods, they will not be executed when creating the object.
If you need this functionality, you need to use the :: operator mentioned in Chapter 2. It allows You 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 first few of PHP The same is true for the version, and this method is still valid today. That is: if you name a class Animal and create a method named Animal in it, then this method is a constructor. If a class also has a __construt construct Functions and functions 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 method 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 constructors in PHP an access method like 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 Singleton pattern.

PHP destructors are the opposite of 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 related to the object. Destructors allow you to execute arbitrary code to clear memory after using an object.

PHP destructors are called when PHP determines that your script is no longer associated with the object. In a function's namespace Internally, this happens when the function returns. For global variables, this happens when the script ends. If you want to explicitly destroy an object, you can assign any other value to the variable pointing to the object. Usually the variable assignment is Is NULL or calls unset.

In the following example, count the number of objects instantiated from the class. The Counter class starts incrementing from the constructor and decrements in the PHP destructor.

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 instances are the components placed on the assembly line. New takes 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><span class="tag"><</span><span> ?php   </span></span></li><li><span>class Counter { private static $</span><span class="attribute">count</span><span> = </span><span class="attribute-value">0</span><span>;   </span></li><li class="alt"><span>function __construct() { self::$count++; }   </span></li><li><span>function __destruct() { self::$count--; }   </span></li><li class="alt"><span>function getCount() { return self::$count; } }   </span></li><li><span>//建立第一个实例  </span></li><li class="alt"><span>$</span><span class="attribute">c</span><span> = </span><span class="attribute-value">new</span><span> Counter();  </span></li><li><span> //输出1   </span></li><li class="alt"><span>print($c-</span><span class="tag">></span><span>getCount() . "</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span>n");   </span></span></li>
<li><span>//建立第二个实例   </span></li>
<li class="alt">
<span>$</span><span class="attribute">c2</span><span> = </span><span class="attribute-value">new</span><span> Counter();   </span>
</li>
<li><span>//输出2   </span></li>
<li class="alt">
<span>print($c-</span><span class="tag">></span><span>getCount() . "</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span>n");   </span>
</li>
<li><span>//销毁实例   </span></li>
<li class="alt">
<span>$</span><span class="attribute">c2</span><span> = </span><span class="attribute-value">NULL</span><span>;   </span>
</li>
<li><span>//输出1  </span></li>
<li class="alt">
<span> print($c-</span><span class="tag">></span><span>getCount() . "</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span>n");   </span>
</li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>

When you create a new PHP destructor 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/446089.htmlTechArticleFirst time learning. If you declare a function in a class and name it __construct, this function will be treated as a Constructor and is executed when an object instance is created. To be clear, _...
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