什么决定什么类对象在PHP中销毁? [英] What determines when a class object is destroyed in PHP?

查看:140
本文介绍了什么决定什么类对象在PHP中销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有 CFoo 类。在下面的示例中, CFoo :: __ destruct()被调用?

  function MyPHPFunc()
{
$ foo = new CFoo();

。 。 。

//何时/在哪里/ $ foo如何销毁/删除?
}

在这个例子中,当脚本退出 MyPHPFunc 因为 $ foo 将无法访问?

解决方案

在PHP中,所有的值都保存在所谓的 zval 中。那些 zval 包含实际数据,类型信息和 - 这对您的问题很重要 - 引用计数。请查看以下代码段:

  $ a = new B; // $ a指向zval(new B),refcount = 1 
$ b = $ a; // $ a,$ b指向zval(new B),refcount = 2(+1)
$ c = $ b; // $ a,$ b,$ c指向zval(new B),refcount = 3(+1)
unset($ a); // $ b,$ c指向zval(new B),refcount = 2(-1)

refcount 到达 0 后, zval refcount 达到

0




  • unset 变量:

      $ a = new B; // refcount = 1 
    unset($ a); // refcount = 0 => __destruct!

    但:

     code> $ a = new B; // refcount = 1 
    $ b = $ a; // refcount = 2
    unset($ a); // refcount = 1 => no destruct as refcount> 0,即使unset()被调用!


  • 离开功能(或方法)范围

      function a(){
    $ a = new B; // refcount = 1
    } // refcount = 0 => __destruct! (因为$ a不再存在)


  • 脚本执行结束

      $ a = new B; // refcount = 1 
    die(); // refcount = 0 => __destruct! (在脚本执行结束所有vars被释放)
    //不需要是die(),可以正常执行结束




这些显然不是导致减少 refcount 的所有条件,



此外,我应该提及,因为PHP 5.3循环引用也将被检测到。因此,如果对象 $ a 引用对象 $ b $ b 参考 $ a ,并且没有任何进一步引用 $ a $ b refcount 将会是 1 ,但它们仍然会被释放 __ destruct ed)。在这种情况下,虽然销毁的顺序是未定义的行为。


Let's say that we have class CFoo. In the following example when is CFoo::__destruct() called?

function MyPHPFunc()
{
  $foo = new CFoo();

  . . .

  // When/where/how does $foo get destroyed/deleted?
}

In this example would the destructor be called when the script exits the scope of MyPHPFunc because $foo would no longer be accessible?

解决方案

In PHP all values are saved in so called zvals. Those zvals contain the actual data, type information and - this is important for your question - a reference count. Have a look at the following snippet:

$a = new B; // $a         points to zval(new B) with refcount=1
$b = $a;    // $a, $b     point to  zval(new B) with refcount=2 (+1)
$c = $b;    // $a, $b, $c point to  zval(new B) with refcount=3 (+1)
unset($a);  //     $b, $c point to  zval(new B) with refcount=2 (-1)

As soon as the refcount reaches 0 the zval is freed and the object destructor is called.

Here are some examples of the refcount reaching 0:

  • unseting a variable:

    $a = new B; // refcount=1
    unset($a);  // refcount=0 => __destruct!
    

    But:

    $a = new B; // refcount=1
    $b = $a;    // refcount=2
    unset($a);  // refcount=1 => no destruct as refcount > 0, even though unset() was called!
    

  • leaving function (or method) scope

    function a() {
        $a = new B; // refcount=1
    }               // refcount=0 => __destruct! (as $a does not exist anymore)
    

  • script execution end

    $a = new B; // refcount=1
    die();      // refcount=0 => __destruct! (on script execution end all vars are freed)
    // doesn't need to be die(), can be just normal execution end
    

These obviously are not all conditions leading to a reduction of refcount, but the ones you will most commonly meet.

Also I should mention that since PHP 5.3 circular references will be detected, too. So if object $a references object $b and $b references $a and there aren't any further references to $a or $b the refcounts of both will be 1, but they still will be freed (and __destructed). In this case though the order of destruction is undefined behavior.

这篇关于什么决定什么类对象在PHP中销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆