Simple question:
I've been exploring open source code and saw the following statement:
$this->{$worker}
What is the meaning of enclosing brackets around, and what is the difference between this and:
$this->worker
Simple question:
I've been exploring open source code and saw the following statement:
$this->{$worker}
What is the meaning of enclosing brackets around, and what is the difference between this and:
$this->worker
Curly braces are used for string or variable interpolation in PHP.
Something like
$worker = 'foo';
$this->{$worker} = 'bar';
that means
$this->foo = 'bar';
class RandomName
{
protected $foo;
protected $bar;
protected $foo_bar;
$properties_array = array('foo', 'bar', 'foo_bar');
if (in_array($property, $properties_array)) {
$this->{$property} = //some value
}
}
The first one actually uses the value of the variable $worker
, while the latter uses the Expression / word worker
to target the object property.
The first example is using a variable as the attribute name. The second example is using a non-variable name for an attribute on your class.