Search code examples
phpgetter-setter

PHP __set magic method


I just came across this piece of code, but I can't seem to understand what the body means:

public function __set($propName, $propValue)
{
    $this->{$propName} = $propValue;
}

what does $this->{$propName} do?


Solution

  • $this->{$propName} accesses property named $propName. If $propName === 'name' then $this->{$propName} is the same as $this->name.

    More information here: http://php.net/manual/en/language.variables.variable.php