I would say, typically this isn't necessary, as you can pass around a
reference to the nested objects that you want to update or you map
strings to objects.
If you have a large app which requires this type of dynamic path-based
access, you might consider using an XML document data structure and
querying it with XPath queries.
In general, your example could be directly implemented using either
eval:
$str = 'parts[0]->parts[1]';
eval("\$val = \$this->struct->$str;");
print_r($val);
Or with a function getNestedProperty like so:
<?
function getNestedProperty($obj, $path)
{
{
$val = $obj;
foreach ($path as $part) {
$val = is_object($val) ? $val->$part :
$val[$part];
}
return $val;
}
class c
{
function c()
{
$this->arr = array('a' =array(1, 2, 3));
}
}
print_r($val = getNestedProperty(new c,
array('arr', 'a', 1)));
?>
Regards,
John Peters
On May 28, 8:51 am, Hugh Oxford <ares...@fas.comwrote:
I want to build a string to reference an object.
I can reference is manually thus:
print_r($this->struct->parts[0]->parts[1]);
but if I build a string...
$string = "->parts[0]->parts[1]"
...and try to reference it thus...
print_r($this->struct{$string});
I get Fatal error : Cannot use object of type stdClass as array
Any thoughts?