Home > Article > Backend Development > Sample code sharing of php destructor
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 whencreating 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.
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'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!