EldinPHP EldinPHP - 1 month ago 6
PHP Question

$object->$attribute instead of $object->attribute ... why?

I have this foreach loop that loops through object's attributes and if there's an attribute, it assigns value to it

foreach ($record as $attribute=>$value){

if ($object->has_attribute($attribute)){


$object->$attribute = $value;


}

}


but what I don't understand is : why we have to use $object->$attribute

if I use $object->attribute which seems more logical to me, because it's basic OOP, my script doesn't work..

I just want to know WHY? Please some insights..

thanks

Fky Fky
Answer

Basically, it's a dynamical attribution :

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