EldinPHP EldinPHP - 1 year ago 65
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..


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