php - Adding to a class' variables with elements in an object -
$class = new class; $foo = json_decode($_post['array']);   in highly contrived example, have class own functions , variables, blah blah.
i decoded json string, values in$foo. how move elements in $foo on $class, that:
$foo->name becomes $class->name?
would trivial if knew elements were, yes... except sake of being dynamic, let's want them transferred over, , don't know names.
you use get_object_vars:
$vars = get_object_vars($foo); foreach ($vars $key => $value) {     $class->$key = $value; }   you implement in class:
public function bindarray(array $data) {     foreach ($data $key => $value) {         $this->$key = $value;     } }   and cast object array:
$obj->bindarray( (array) $foo );   or add method too:
public function bindobject($data) {      $this->bindarray( (array) $data ); }      
Comments
Post a Comment