EldinPHP EldinPHP - 1 month ago 5
PHP Question

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

There is a

foreach
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
$object->$attribute
instead of
$object->attribute
? 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

Basically, it's a dynamical attribution :

$attribute = 'toto'; 
$object->$attribute = 'tata';
echo $object->toto; // will display 'tata'
Comments