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

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

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

asp.net mvc - breakpoint on javascript in CSHTML? -