By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,223 Members | 1,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,223 IT Pros & Developers. It's quick & easy.

Build string to reference object

P: n/a
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?
Jun 2 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hugh Oxford 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?
True. You can't do it that way. You could use eval() - but that's a
bad thing to use.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 2 '08 #2

P: n/a
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?
Jun 2 '08 #3

P: n/a
petersprc wrote:
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?
Thanks Peter and for your other replies elsewhere.
Jun 2 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.