且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP销毁两次

更新时间:2023-02-05 11:21:56

调用destruct不会破坏对象.第一次使用__destruct()调用它,然后在PHP脚本终止时,它在清理时再次调用它.

Invoking destruct doesn't destroy the object. You call it with __destruct() the first time, then when the PHP script terminates, it calls it again at cleanup.

如果要在终止脚本之前销毁对象,请unset().您应该只看到一个销毁调用.

If you're looking to destroy the object prior to script termination, unset() it. You should see only a single destruct call made.

具体来说,您的类B创建一个类A的自包含实例.由于B还将通过__call()进行的方法调用路由到A对象,因此,B上的__destruct()调用正在A上调用__destruct()的原因; B没有定义析构函数,并将调用向上传递.

Specifically, your class B creates a self contained instance of class A. Since B also routes method calls via __call() to the A object, that's why a __destruct() call on B is calling __destruct() on A; B has no destructor defined and passes the call up.