Same as (discarding protected and closed member):
foreach ($obj as $property_name => $property_value) {
if (array_key_exists($property_name, $arr))
$obj->$property_name = $arr[$property_name];
}
Or just add the iterate method to class A:
class A {
public $a;
public $b;
function iterate($array) {
foreach ($this as $property_name => $property_value) {
if (array_key_exists($property_name, $array))
$this->$propety_name = $array[$property_name];
}
}
function f1 () {
}
}
and use the iterate () method.
source
share