0

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
  • Well you need to enable error logging to find out if not a fatal error is happening in that _save function actually. Unless you don't do, it's hard to say what is triggering the connection reset. I assume it's a PHP error in the shutdown phase. Commented May 23, 2012 at 23:07

2 Answers 2

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.

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

Comments

-1

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

6 Comments

I know it has nothing to do with my browser. I only get this kind of error. Like I said, _save() method is not returning any Exceptions. The __destruct() should have no problems as all references required should still be present.
Its does not have to return ... did you implement anyway ??? Am only working form the limited information you provided
_save() method is calling some model methods that save information to database. This method works without any errors, warnings whatsoever. When called manually it works. When called by register_shutdown_function also. I only have problems when it's called by destructor.
To make it even more weird. All data is saved if I place there exit() after _save() in _destruct().
Basically I can call whatever code I like after the _save() in the _destruct(), but something is happening because I call _save(). Probably some interaction with database. Maybe because database is a singleton also?
|

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.