I'm trying to write a simple class that wrap $_SESSION. The idea is that I'll be able to set and get values from the class that will refer to the $_SESSION variable, having the __set and __get execute some other stuff I need.
What I currently have, which is nowhere near complete:
class Session
{
// Property declarations
public $data = array();
/**
* Class initialization
*/
public function __construct()
{
if (!session_id()) {
ini_set('session.cookie_lifetime', 0);
ini_set('session.gc-maxlifetime', 0);
session_start();
}
$this->data =& $_SESSION;
}
/**
* Get session ID
* @return string
*/
public function getId()
{
return session_id();
}
}
I'm trying to make it work as follows:
$session->images['directory'] = 1;
will be the same as
$_SESSION['images']['directory'] = 1;
and
echo $session->images['directory'];
should in turn output: 1
The catch is, I want to use multidimensional arrays, like so:
$session->images['foo']['bar'] = 'works';
With regular __set and __get functions provided in many examples around SO and php.net, this doesn't work.
Ofcourse, after a page reload/navigation, I'll be able to access the same variables with their respective values.
What I've tried is search Google and Stackoverflow, I keep landing on "ArrayAcces", but cannot seem to figure out how to use that with my problem.
Anyone point me in the right direction?