问题
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