PHP __destruct is causing net::ERR_CONNECTION_RESET register_shutdown_function is not. What's the difference?

你说的曾经没有我的故事 提交于 2019-12-12 02:29:45

问题


so I have following problem. I have a session class that should save it's data to database at the end of the request execution. Basically, when it is destructed. I'm using singleton pattern in this case. I have a destructor like this:

public function __destruct()
{
    $this->_save(); // _save is public
    // exit('I can reach this point with no error');
}

But with that code I get net::ERR_CONNECTION_RESET from chrome and other browsers. If I comment out the destructor and place this in constructor:

register_shutdown_function(array($this, '_save'));

The _save method is not returning any Exceptions when I call it directly.

Everything works fine. What could be wrong and why?

Thanks!


回答1:


Ok, I found the solution. The _save() method was calling some methods that were saving data to database. BUT! The database instance is a SINGLETON, so there were no references anywhere and the database object was already destroyed at this point. The solution was to save the reference to database instance somewhere in the model. Looks like register_shutdown_function was working because nothing was destroyed yet.




回答2:


your error has nothign to do with crome or other browsers.

if you look at the PHP documentation http://php.net/manual/en/language.oop5.decon.php

The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

A. If "exit" is called somewhere

B. FATAL error somewhere in the code

C. If there is an exception thrown in another destructor

D. If you try to HANDLE an exception in a destructor

$this->_save() is a method and it might be calling Exception one way or the other

While register_shutdown_function http://php.net/manual/en/function.register-shutdown-function.php

Registers a callback to be executed after script execution finishes or exit is called

I would assume that this would work weather the script terminates properly or not



来源:https://stackoverflow.com/questions/10155073/php-destruct-is-causing-neterr-connection-reset-register-shutdown-function-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!