I am having trouble understanding when and why you need to use $this->$property. So adding the $
to both the the this
keyword and the property
. I have only seen this used within the magic methods __get()
and __set()
. Can anybody elaborate?
You can use $this->$property
when $property
contains a name of a property or $this->$function()
when $function
contains a function name.
Example:
class MyClass {
private $email = "rr@rr.com";
public function getProperty($p){
return $this->$p;
}
}
$obj = new MyClass;
$obj->getProperty("email"); // Returns rr@rr.com