2
为什么new $this
有效?我明白new self
或new static
但不能为$this
发现了什么:
class Foo {
private $str;
public function __construct($str) {
$this->str = $str;
}
public function test($str) {
return new $this($str);
}
}
$bar = new Foo('bar');
var_dump($bar->test('TEST'));
*“,但无法找到任何东西$这”* - 哦? http://php.net/manual/en/language.oop5.basic.php *“在一个对象上下文中调用一个方法时,可以使用伪变量$ this''$ this'是对调用对象(通常是该方法所属的对象,但如果该方法是从次级对象的上下文静态调用的,则可能是另一个对象)。“* –