I have a class like this:
class Session {
private Session_Database $db;
public function __construct(){
// Instantiate new Database object
$this->db = new Session_Database();
// Set handler to overide SESSION
session_set_save_handler(
[$this, '_open'],
[$this, '_close'],
[$this, '_read'],
[$this, '_write'],
[$this, '_destroy'],
[$this, '_gc']
);
// Start the session
session_start();
}
/* ... */
public function _close(): bool {
// Close the database connection
$this->db->close();
return true;
}
/* ... */
}
I'm seeing this error in the logs:
PHP Fatal error: Uncaught Error: Cannot access private property
Session::$db
in [line number of$this->db->close()
]Stack trace:
#0 [internal function]:Session->_close()
#1 {main}\n thrown in ...
However when I test the code by stepping through in xdebug it works as expected.
What is going on?
session_set_save_handler()
you make certain methods from your session class callable, but when they are called they are not called from within the session object. Have a look at the User Contributed Notes._close()
is even getting called, but when I test, that isn't actually what happens.close
method of their PDO handler. To me your code appears correct.