Re: Build string to reference object



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...@xxxxxxx> wrote:
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?

.



Relevant Pages

  • Re: Build string to reference object
    ... strings to objects. ... If you have a large app which requires this type of dynamic path-based ... I can reference is manually thus: ... Cannot use object of type stdClass as array ...
    (comp.lang.php)
  • Build string to reference object
    ... I want to build a string to reference an object. ... Cannot use object of type stdClass as array ...
    (comp.lang.php)
  • Re: Complex Specified Information - Pitman Formula
    ... between the reference string and the test string. ... I *know* what sequences actually exist or possibly could ... There are only test strings that you compare to ...
    (talk.origins)
  • Re: Complex Specified Information - Pitman Formula
    ... between the reference string and the test string. ... I *know* what sequences actually exist or possibly could ... There are only test strings that you compare to ...
    (talk.origins)
  • Re: Complex Specified Information - Pitman Formula
    ... you go out to analyze any other sequences. ... other reference you give. ... is it possible for a set of reference strings to miss a non- ... sequences is likely to be non-random in origin. ...
    (talk.origins)