Home >Backend Development >PHP Tutorial >Sample code sharing of php destructor

Sample code sharing of php destructor

黄舟
黄舟Original
2017-07-02 10:18:201149browse

How to correctly understand PHPDestructor

If you declare a function in a class and name it construct, this function will be regarded as a constructor and is executed when creating an object instance. To be clear, it is two underscores. Like any other function, the constructor may have parameters or default values. You can define a class to create an object and set all its properties. Put it in a 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 powerful feature of classes. One class (subclass/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. You can also override the methods and properties of the base class. As shown in 3.1.2, 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 are not executed when

creating the object.If you need this functionality, 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 ​​name the constructor after the class. The same was true for previous versions of PHP, and this method still works. That is: if you name a class Animal and create a If the name is also an Animal method, then this method is a 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 makes classes written in previous PHP versions It can still be used. But new scripts (PHP5) should use construct.

PHP's new way of declaring a constructor allows the constructor to have a unique name, regardless of the name of the class in which it is located. What. In this way, when you change the name of the class, you don't need to change the name of the constructor.

You may give the constructor an access method in PHP like other class methods. The access method will affect the subsequent The ability to instantiate objects within a certain scope. This allows the implementation of 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 memory occupied by the object's properties and destroys the resources associated with the object. The destructor allows you to execute arbitrary code to clear the memory after using an object.

When PHP decides that your script is no longer The PHP destructor will be called when associated with an object. 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, count the number of objects instantiated from the class. Counter class Values ​​are added starting from the constructor and decremented 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 diagram, and the instance is the placeholder. Components on the assembly line. New takes the name of the class and returns an instance of that class. If the constructor requires parameters, you should enter the parameters after new.

 1 < ?php   
 2  3 class Counter { private static $count = 0;   
 4  5 function construct() { self::$count++; }   
 6  7 function destruct() { self::$count--; }   
 8  9 function getCount() { return self::$count; } }   
10 11 //建立第一个实例  12 13 $c = new Counter();  
14 15  //输出1   16 17 print($c->getCount() . "<br>\n");   
18 19 //建立第二个实例   20 21 $c2 = new Counter();   
22 23 //输出2   24 25 print($c->getCount() . "<br>\n");   
26 27 //销毁实例   28 29 $c2 = NULL;   
30 31 //输出1  32 33  print($c->getCount() . "<br>\n");   
34 35 ?>

When you create a new PHP destructor Instances, 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.

PHP 5 allows Developers define a method as a constructor in a class. Classes with a constructor will call this method every time a new object is created, so it is very suitable for doing some initialization work before using the object.

Note: If a constructor is defined in a subclass, the constructor of its parent class will not be called implicitly. To execute the parent class's constructor, you need to call parent::construct() in the child class's constructor. If the subclass does not define a constructor, it will be inherited from the parent class like an ordinary class method (if it is not defined as private).

Example #1 Using the new standard constructor

<?phpclass BaseClass {  
function construct() {       
print "In BaseClass constructor\n";
   }
}class SubClass extends BaseClass {   
function construct() {
       parent::construct();       
       print "In SubClass constructor\n";
   }
}class OtherSubClass extends BaseClass {    
// inherits BaseClass&#39;s constructor}
// In BaseClass constructor$obj = new BaseClass();
// In BaseClass constructor
// In SubClass constructor$obj = new SubClass();
// In BaseClass constructor$obj = new OtherSubClass();?>

For backward compatibility, if PHP 5 cannot find construct() in the class function and does not inherit one from the parent class, it will try to find an old-style constructor, that is, a function with the same name as the class. Therefore, the only situation where compatibility issues will arise is when there is already a method named

construct() in the class but it is used for other purposes.

与其它方法不同,当 construct() 被与父类 construct() 具有不同参数的方法覆盖时,PHP 不会产生一个 E_STRICT 错误信息

自 PHP 5.3.3 起,在命名空间中,与类名同名的方法不再作为构造函数。这一改变不影响不在命名空间中的类。

Example #2 Constructors in namespaced classes

<?php
namespace Foo;
class Bar {
    public function Bar() {
        // treated as constructor in PHP 5.3.0-5.3.2
        // treated as regular method as of PHP 5.3.3
    }
}
?>


析构函数 ¶

void destruct ( void )

PHP 5 引入了析构函数的概念,这类似于其它面向对象的语言,如 C++。析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。

Example #3 析构函数示例

<?php
class MyDestructableClass {   
function construct() {       
print "In constructor\n";       
$this->name = "MyDestructableClass";
   }   
   function destruct() {       
   print "Destroying " . $this->name . "\n";
   }
}$obj = new MyDestructableClass();?>

和构造函数一样,父类的析构函数不会被引擎暗中调用。要执行父类的析构函数,必须在子类的析构函数体中显式调用 parent::destruct()。此外也和构造函数一样,子类如果自己没有定义析构函数则会继承父类的。

析构函数即使在使用 exit() 终止脚本运行时也会被调用。在析构函数中调用 exit() 将会中止其余关闭操作的运行。

The above is the detailed content of Sample code sharing 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