_set and _get in PHP5

hi

what is _set and _get? can anbody share wht is the practical use of this methods? what situations we can use this methods? what is the advantage of these functions?

I am not able to find the answer

Please help me

Thanks

http://www.php.net/manual/en/language.oop5.overloading.php

__set and __get methods can be used to run custom code on accessing and setting class members. There’s an example in the link above.

Thanks

But i am not able to understand the concept.

please help me

OK, here’s a very simple example:


class Foo
{
    private $x;

    public function __set($name, $value)
    {
        $this->x[$name]=str_replace('dog','cat',$value);
    }

    public function __get($name)
    {
        return $this->x[$name];
     }

}

$foo=new Foo;
$foo->a='I have a dog';

echo $foo->a; //prints I have a cat

So it can be used as a data processing mechanism for a class. Consider the following for a comparison:


class Foo
{
    private $a;

    public function setA($value)
    {
        $this->a=str_replace('dog','cat',$value);
    }

    public function getA()
    {
        return $this->a;
     }

}

$foo=new Foo;
$foo->setA('I have a dog');

echo $foo->getA(); //prints I have a cat