/    Sign up×
Community /Pin to ProfileBookmark

how to perform dynamic cascading references ??

Hello forums!!
I would like to how to access nested objects
supppose i had an array

[code=php]$array = array(“parent_obj”, “child_obj1”, “child_obj2”);[/code]

I would like to perform referencing as per array ie

[code=php]$this->parent_obj->child_obj1->child_obj2;[/code]

I dont know how to perform dynamic cascading references.
Can anybody make a help on this.
Any help / comment/ suggestions are warmly welcome.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@PHPychoauthorFeb 25.2008 — Thanks i got it working by performing as:

[code=php]$element = '';
foreach ($array as $obj){
$element .= '->'.$obj;
}
$element = trim($element, "->");

eval('$return = $this->'.$element.';');

return $return;[/code]
Copy linkTweet thisAlerts:
@MrCoderFeb 25.2008 — Your welcome ?
Copy linkTweet thisAlerts:
@ZnupiFeb 25.2008 — Here's a better alternative, since eval() isn't that good:
[code=php]
$array = Array("parent", "child1", "child2");
class emptyclass {

}
$object = new emptyclass();
$temp = $object;
foreach ($array as $element) {
$temp->$element = new emptyclass();
$temp = $temp->$element;
}
print_r($object);
[/code]

?
×

Success!

Help @PHPycho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...