Php Destruct被称为两次
发布时间:2020-12-13 22:21:36  所属栏目:PHP教程  来源:网络整理 
            导读:下面的代码说明了两次调用destruct().我想知道为什么? class A { function hi(){ echo 'hi'; } function __destruct(){ echo 'destroy'; }}class B{ public $this_ = ''; function __construct(){ $this-this_ = new A; } function __call($method,$params)
                
                
                
            | 
 下面的代码说明了两次调用destruct().我想知道为什么? 
  
  
  class A {
    function hi(){ echo 'hi'; }
    function __destruct(){
        echo 'destroy';
    }
}
class B{
    public $this_ = '';
    function __construct(){
        $this->this_ = new A;
    }
    function __call($method,$params) {
          return call_user_func_array(array($this->this_,$method),$params);
    }
}
$b = new B;
$b->__destruct();输出: destroydestroy 编辑 zneak和TomcatExodus都是正确的.如果我只是: [..code..] $b = new B; $b->__destruct(); print 'end of script'; 输出将显示: destroyend of scriptdestroy 解决方法
 调用destruct不会破坏对象.您第一次使用__destruct()调用它,然后当PHP脚本终止时,它会在清理时再次调用它. 
  
  如果您希望在脚本终止之前销毁对象,请取消设置()它.你应该只看到一次破坏性调用. 具体来说,你的类B创建一个自包含的类A实例.由于B也通过__call()将方法调用路由到A对象,这就是为什么B上的__destruct()调用在A上调用__destruct(); B没有定义析构函数并通过调用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
