I have a function, that should read array and dynamically set object properties.
class A {
public $a;
public $b;
function set($array){
foreach ($array as $key => $value){
if ( property_exists ( $this , $key ) ){
$this->{$key} = $value;
}
}
}
}
$a = new A();
$val = Array( "a" => "this should be set to property", "b" => "and this also");
$a->set($val);
Well, obviously it doesn't work, is there a way to do this?
EDIT
It seems that nothing is wrong with this code, the question should be closed
$this->$key = $value;