I am using a magic getter/setter class for my session variables, but I don't see any difference between normal setters and getters.
The code:
class session
{
public function __set($name, $value)
{
$_SESSION[$name] = $value;
}
public function __unset($name)
{
unset($_SESSION[$name]);
}
public function __get($name)
{
if(isset($_SESSION[$name]))
{
return $_SESSION[$name];
}
}
}
Now the first thing I noticed is that I have to call $session->_unset('var_name')
to remove the variable, nothing 'magical' about that.
Secondly when I try to use $session->some_var
this does not work. I can only get the session variable using $_SESSION['some_var']
.
I have looked at the PHP manual but the functions look the same as mine.
Am I doing something wrong, or is there not really anything magic about these functions.
First issue, when you call
unset($session->var_name);
It should be the same as calling
$session->_unset('var_name');
Regarding not being able to use __get(); What doesn't work? What does the variable get set to and what warnings are given. Ensure you have set error_reporting()
to E_ALL.
It may also be a good idea to check you have called session_start