Home  >  Article  >  Backend Development  >  Correct understanding of php destructor

Correct understanding of php destructor

黄舟
黄舟Original
2017-07-02 10:10:371265browse

If you declare a function in a class named construct, this function will be treated as a constructor function and will be executed when creating an object instance. To be clear, there are two underscores . 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 PHPDestructor.

Inheritance is a powerful feature of classes. A class (child Class/derived class) can inherit the functions of another class (parent class/base class). The derived class will contain all properties and methods of the base class, and can add other properties and methods to the derived class. 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 wonder how constructors are inherited When they are inherited together with other methods, they will not be executed when creating the 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, and you can use parent::construct to call the parent class's constructor.

Some object-oriented languages ​​​​are after the class Named constructors. The same was true for previous versions of PHP, 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 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.

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 is. In this way, when you change the name of the class, you will not Need to change the name of the constructor.

You may give the constructor 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 to implement some Fixed Design patterns, such as the Singleton pattern.

PHP destructors, as opposed to constructors. PHP calls them to destroy an object from memory. By default, PHP only releases the object The memory occupied by the 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 determines that your script is no longer related to the object, PHP analyzes The constructor 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 give a pointer to the object. Assign any other value to the variable. Usually assign the variable to NULL or call unset.

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

Once you define a class, you can use new to create an instance of this 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 constructor requires parameters, you should enter the parameters after new.

< ?php   
class Counter { private static $count = 0;   
function construct() { self::$count++; }   
function destruct() { self::$count--; }   
function getCount() { return self::$count; } }   
//建立第一个实例  
$c = new Counter();  
 //输出1   
print($c->getCount() . "<br>\n");   
//建立第二个实例   
$c2 = new Counter();   
//输出2   
print($c->getCount() . "<br>\n");   
//销毁实例   
$c2 = NULL;   
//输出1  
 print($c->getCount() . "<br>\n");   
?>

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

The above is the detailed content of Correct understanding of php destructor. For more information, please follow other related articles on the PHP Chinese website!

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