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?
1 Answer
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 = "[email protected]";
public function getProperty($p){
return $this->$p;
}
}
$obj = new MyClass;
$obj->getProperty("email"); // Returns [email protected]
3 Comments
jcromanu
Agree with @Richard Reiber
Juan Rangel
Ahh I see. So referencing the property/method? Thank you sir.
Richard
@JuanRangel: Yes. No problem. You can also see the documentation.
$var = "ThisIsMyProperty";) then you use it like this:$this->$var?!