PHP __destruct calls net :: ERR_CONNECTION_RESET register_shutdown_function no. Who cares?

so i have the following problem. I have a session class that should store its data in the database at the end of the query. In principle, when it is destroyed. In this case, I am using a singleton template. I have such a destructor:

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

But with this code, I get net :: ERR_CONNECTION_RESET from chrome and other browsers. If I comment on the destructor and put it in the constructor:

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

The _save method does not return any Exceptions when I call it directly.

Everything works perfectly. What could be wrong and why?

Thanks!

0
source share
2 answers

Ok, I found a solution. The _save () method called some methods that stored data in a database. BUT! The database instance is SINGLETON, so there were no references, and the database object was already destroyed at this point. The solution was to keep the link to the database instance somewhere in the model. Register_shutdown_function seems to work because nothing has been destroyed.

+1
source

your error is not related to 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

C. FATAL error somewhere in code

C. If another destructor has an exception,

E. If you try to THROW an exception in the destructor

$this->_save() is a method, and it may throw an Exception anyway

So far 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 suggest that this would work the weather script ends correctly or not

-1
source

All Articles