php destructor behaviour

前端 未结 5 1663
一个人的身影
一个人的身影 2020-12-16 00:23

im trying to understand php constructor and destructor behaviour. Everything goes as expected with the constructor but i am having trouble getting the destructor to fire imp

5条回答
  •  遥遥无期
    2020-12-16 01:01

    The __destruct() magic function is executed when the object is deleted/destroyed (using unset). It is not called during shutdown of a script. When a PHP script finishes executing, it cleans up the memory, but it doesn't 'delete' objects as such, thus the __destruct() methods aren't called.

    You may be thinking of the register_shutdown_function(), which is fired when your PHP script finishes executing.

    function shutdown()
    {
        // code here
        echo 'this will be called last';
    }
    
    register_shutdown_function('shutdown');
    

提交回复
热议问题