views:

240

answers:

3

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.

+1  A: 

I thought getters and setters were for variables inside the class?

class SomeClass {
    private $someProperty;

    function __get($name) {
        if($name == 'someProperty') return $this->someProperty;
    }

    function __set($name, $value) {
        if($name == 'someProperty') $this->someProperty = $value;

    }
}


$someClass = new SomeClass();
$someClass->someProperty = 'value';
echo $someClass->someProperty;

?

Ropstah
This is a question not an answer. Secondly, magic functions are that. Functions. The restrictions as to what they can access are the same as normal functions so there is no reason why they can't return a value from an external source. For example Doctrine gets and sets values from databses.
Yacoby
No you don't have to define the variable inside the function anymore. That is one part of the 'magic' I did understand. When you just call $someclass->variable = 10; it will make that variable if it does not exist.
Saif Bechan
+1  A: 

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

Yacoby
Oh yes the the set and the get are working now. I was referencing the wrong variables for my test. +1 And thank you for the right syntax on the unset method. Will this syntax: unset($session->var_name); not work if there is no magic unset?
Saif Bechan
@Saif No, it doesn't. If you want to use `unset` you must define the `__unset` function.
Yacoby
At least it would not have an effect on _SESSION without that __unset() method.
VolkerK
Ok its all clear to me now. I will run some test myself with it also. At least I now know that I am at the right track!
Saif Bechan
+1  A: 
class session { /* ...as posted in the question ... */ }

session_start();
$s = new session;
$s->foo = 123;
$s->bar = 456;
print_r($_SESSION);

unset($s->bar);
print_r($_SESSION);

prints

Array
(
    [foo] => 123
    [bar] => 456
)
Array
(
    [foo] => 123
)

Ok, maybe not "magical". But works as intended.
If that's not what you want please elaborate...

VolkerK
Yes this is what i want. I made some stupid mistakes in my code, sorry for this. This works just fine only the right syntax for the unset was not clear for me. It works this way however.
Saif Bechan
+1 for a nice testcase
Saif Bechan