EldinPHP EldinPHP - 1 year ago 64
PHP Question

What is the difference between $object->$attribute and $object->attribute?

There is a

loop that iterates attributes. If an object has the next attribute, the code assigns a value for this attribute:

foreach ($record as $attribute=>$value) {
if ($object->has_attribute($attribute)) {
$object->$attribute = $value;

I don't understand why we have to use
instead of
? The latter seems more logical to me because it looks like basic OOP, but in this case the script doesn't work.

I just want to know why. Please give me some insights.

Fky Fky
Answer Source

Basically, it's a dynamical attribution :

$attribute = 'toto'; 
$object->$attribute = 'tata';
echo $object->toto; // will display 'tata'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download