http://www.php.net/manual/en/reflectionproperty.setvalue.php
You can using Reflection
, I think.
<?php
function set(array $array) {
$refl = new ReflectionClass($this);
foreach ($array as $propertyToSet => $value) {
$property = $refl->getProperty($propertyToSet);
if ($property instanceof ReflectionProperty) {
$property->setValue($this, $value);
}
}
}
$a = new A();
$a->set(
array(
'a' => 'foo',
'b' => 'bar'
)
);
var_dump($a);
Outputs:
object(A)[1]
public 'a' => string 'foo' (length=3)
public 'b' => string 'bar' (length=3)