Let's say we have an object $obj. This object has a property which is as follows:
$obj->p1->p2->p3 = 'foo';
Now I get the nested property structure in an array:
$arr = array( 'p1', 'p2', 'p3' );
Currently I use the following function to access the property accordingly:
function getProperty( $obj, $property ) {
foreach( $property as $p ) {
$obj = $obj->{$p};
}
return $obj;
}
$value = getProperty( $obj, $arr); // = 'foo'
Is there a smarter way to do that (no, 'eval' is not an option! ;) )?