何时使用 $this->$属性
When to use $this->$property
我无法理解您何时以及为何需要使用 $this->$属性。因此,将 $
添加到 this
关键字和 property
中。我只在魔术方法 __get()
和 __set()
中看到过它。有人可以详细说明吗?
当 $property
包含一个 属性 的名称时,您可以使用 $this->$property
或当 $function
包含一个函数名称时,您可以使用 $this->$function()
。
示例:
class MyClass {
private $email = "rr@rr.com";
public function getProperty($p){
return $this->$p;
}
}
$obj = new MyClass;
$obj->getProperty("email"); // Returns rr@rr.com
我无法理解您何时以及为何需要使用 $this->$属性。因此,将 $
添加到 this
关键字和 property
中。我只在魔术方法 __get()
和 __set()
中看到过它。有人可以详细说明吗?
当 $property
包含一个 属性 的名称时,您可以使用 $this->$property
或当 $function
包含一个函数名称时,您可以使用 $this->$function()
。
示例:
class MyClass {
private $email = "rr@rr.com";
public function getProperty($p){
return $this->$p;
}
}
$obj = new MyClass;
$obj->getProperty("email"); // Returns rr@rr.com