php - Magic method get and set values dynamically from array -


iam sorry if kind of question has been asked before, couldnt find according question.

i've got class uses magic method , set. want use property of array set "name", later access property using "name".

what now:

$arr = array('name' => 'value') $this->obj->name = $arr['name']; 

what want , doesnt work try:

$arr = array('name' => 'value');  foreach($arr $item)    $this->obj->[$item] = $item['name'];  echo $this->obj->name; // result should 'value' 

php pretty magic methods, seems syntactic thing. should able you're after just

foreach ($arr $key => $item)      $this->obj->$key = $item;  echo $this->obj->name; // results in 'value' 

Comments

Popular posts from this blog

php - Passing multiple values in a url using checkbox -

compilation - PHP install fails on Ubuntu 14 (make: *** [sapi/cli/php] Error 1) PHP 5.6.20 -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -