PHP __set 魔法方法

PHP __set magic method

我刚看到这段代码,但我似乎无法理解 body 的含义:

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

$this->{$propName} 是做什么的?

$this->{$propName} 访问名为 $propName 的 属性。如果 $propName === 'name'$this->{$propName}$this->name 相同。

这里有更多信息:http://php.net/manual/en/language.variables.variable.php

花括号导致它们之间的变量被插值。这在很多地方都很有用,但在这个特定的地方它有效地做到了这一点:

// if $propName = 'mike';
$this->{$propName} = 'X';
// Results in:
$this->mike = 'X';

// if $propName = 'cart';
$this->{$propName} = 'full';
// Results in:
$this->cart = 'full';

// if $propName = 'version';
$this->{$propName} = 3;
// Results in:
$this->version = 3;