I am programming a website backend in PHP and I need to encapsulate $_SESSION in a class. I ve made my Session class a singleton but I am having trouble using it.
class Session
{
private static $instance;
public static $sessionID;
private function __construct()
{
session_start();
self::$sessionID = session_id();
}
public function Session() {
return Session::singleton();
}
public static function singleton()
{
if (!isset(Session::$instance)) {
Session::$instance = new Session();
}
return Session::$instance;
}
public function destroy()
{
foreach ($_SESSION as $var => $val) {
$_SESSION[$var] = null;
}
session_destroy();
}
public function __clone()
{
trigger_error( Clone is not allowed for .__CLASS__,E_USER_ERROR);
}
public function __get($var)
{
return $_SESSION[$var];
}
public function __set($var,$val)
{
return ($_SESSION[$var] = $val);
}
public function __destruct()
{
session_write_close();
}
public function exist( $var ) {
return isset($_SESSION[ $var ]);
}
}
Works fine 95% of the time. But occasionnaly when I invoke var_dump(Session::singleton());
output : object(Session)#2 (0) { }
It seems to me obvious that it is making another instance of my Session class, but I don t see exactly how it is possible.
Thanks a lot!