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'是對調用對象(通常是該方法所屬的對象,但如果該方法是從次級對象的上下文靜態調用的,則可能是另一個對象)。「* –