Instead
$this->$key; $this->key=$value;
It should be
$this->$key=$value;
Who cares?
Variable property $this->$key; as a separate instruction, it accesses a property with a name in $key , but does nothing with it. But this will lead to the declaration of the Undefined property if the property has not been previously declared or dynamically assigned.
$this->key refers to the literal property of key , that is, public $key; if it was declared explicitly.
$this->$key refers to a property with the name stored in $key . Therefore, if $key === 'foo' is the same as $this->foo .
Assignment of dynamic properties that were not previously declared does not depend on the use of variable properties. Therefore, if the $foo property does not exist, $this->foo = 1 matches $this->$key = 1 with $key === 'foo' . Both create a public $foo property for the current object.
Special occasions
If $key not a string, it will be passed to the string. If it is an object or resource that cannot be passed to a string, you will receive an inability to convert the string to a string.
If $key converted to an empty string, you will receive a Can not access empty property error.
If $key starts with a number or contains special characters that are usually not allowed in variable names (for example, $ ), a property will be created anyway. Access to properties with such names is always possible if you use {} to use arbitrary expressions as property names. Try the following:
$o = new stdclass; $o->{'!"Β§$%'} = 'it works'; echo $o->{'!"Β§$%'};
source share