__get
and
__set
magic methods allows us to get and set any variable without having individual getter and setter methods:
class Foo {
    protected $properties = array();

    public function __get( $key )
    {
        if(array_key_exists($key, $this->properties)){
            return $this->properties[$key];
        }
        return $this->properties[ $key ];
    }
    public function __set( $key, $value )
    {
        $this->properties[ $key ] = $value;
    }
}
$foo = new Foo();
$foo->name = 'David';

echo $foo->name;
In this example, we can set any variable, even if it does not exist. Every variable will be addd to
properties
array
by Valeri Tandilashvili
4 years ago
PHP
OOP
3
Pro tip: use ```triple backticks around text``` to write in code fences