1
<?php

class A
{
    static private $_instance = null;

    static public function Init()
    {   
        self::$_instance = new A();
    }   

    function __construct()
    {   
        echo "__construct\n";
    }   

    function __destruct()
    {   
        var_dump(debug_backtrace());
        echo "__destruct\n";
    }   
}
$a = A::Init();

Normally, we should get following output: (Yes. I got this result in 2 different servers with PHP 5.2.10-2ubuntu6.10 and PHP 5.3.1)

__construct
array(1) {
  [0]=>
  array(5) {
    ["function"]=>
    string(10) "__destruct"
    ["class"]=>
    string(1) "A"
    ["object"]=>
    object(A)#1 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}
__destruct

But, on another server with CentOS release 5.7 and PHP 5.2.17, I got this :

    __construct
    array(2) {
      [0]=>
      array(7) {
        ["file"]=>
        string(10) "/tmp/1.php"
        ["line"]=>
        int(7)
        ["function"]=>
        string(10) "__destruct"
        ["class"]=>
        string(1) "A"
        ["object"]=>
        object(A)#1 (0) {
        }
        ["type"]=>
        string(2) "->"
        ["args"]=>
        array(0) {
        }
      }
      [1]=>
      array(6) {
        ["file"]=>
        string(10) "/tmp/1.php"
        ["line"]=>
        int(21)
        ["function"]=>
        string(4) "Init"
        ["class"]=>
        string(1) "A"
        ["type"]=>
        string(2) "::"
        ["args"]=>
        array(0) {
        }
      }
    __destruct
    array(1) {
      [0]=>
      array(5) {
        ["function"]=>
        string(10) "__destruct"
        ["class"]=>
        string(1) "A"
        ["object"]=>
        object(A)#2 (0) {
        }
        ["type"]=>
        string(2) "->"
        ["args"]=>
        array(0) {
        }
      }
    }
    __destruct

Why does the function __destruct called twice here? Especially the first time.

I think there might be something special in the configuration, any suggestion?

Thanks.

==================

PS: This problem is not caused by "Singleton design pattern". Same issue appeared with following code :

<?php
class A
{
    function __construct()
    {   
        echo "__construct\n";
    }

    function __destruct()
    {
        var_dump(debug_backtrace());
        echo "__destruct\n";
    }
}
$a = new A(); 
5
  • 3
    @vascowhite: I feel this is not an exact duplicate. Someone who only knows PHP may not understand that example in C++. While the answers may be somewhat similar, the targeted audiences are vastly different. Commented Dec 10, 2011 at 7:30
  • Maybe its releated with garbage collector? Commented Dec 10, 2011 at 7:45
  • FYI, PHP 5.2.17 on mac this code only fires __destruct once. Commented Dec 10, 2011 at 8:35
  • 2
    PHP 5.3.5 on mac also works correctly. Are you sure you have the same code on all servers? Commented Dec 10, 2011 at 8:38
  • @TimG Thanks for you test and reply. If you turn on the setting zend.ze1_compatibility_mode = On in your php.ini, you can find twice __destruct in the results. This might be a bug, see my answer blow please. Commented Dec 11, 2011 at 3:06

1 Answer 1

4

Finally I find the reason.

It might be a bug in PHP 5.2.x :

If

zend.ze1_compatibility_mode = On 

then you can see "__destruct" twice when execute the code I provide in my question.

This issue has been reported in other versions : https://bugs.php.net/bug.php?id=29756

Not affect PHP 5.3 in my test. (PHP 5.3 removed this setting)

Hope this answer will be helpful for some guys later :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.