note 82886 added to language.variables.variable

From: Date: Wed, 30 Apr 2008 13:22:10 +0000
Subject: note 82886 added to language.variables.variable
Groups: php.notes 
Request: Send a blank email to php-notes+get-138936@lists.php.net to get a copy of this message
Trying to use the '->' syntax does not work for variable variables. Example (causes notice level errors): <?php class Cat { public $favfood = 'FISH!'; } class Bag { public $colour = 'brown', $oCat = null; function __construct () { $this->oCat = new Cat(); } } class Planet { public $oBag = null; function __construct() { $this->oBag = new Bag(); } } $oPlanet = new Planet(); $vv1 = 'oBag'; $vv2 = 'oBag->colour'; $vv3 = 'oBag->oCat'; $vv4 = 'oBag->oCat->favfood'; echo('<pre>'); print_r($oPlanet->{$vv1}); # outputs Bag Object print_r($oPlanet->{$vv2}); # outputs Notice: Undefined property: Planet::$oBag->colour print_r($oPlanet->{$vv3}); # outputs Notice: Undefined property: Planet::$oBag->oCat print_r($oPlanet->{$vv4}); # outputs Notice: Planet::$oBag->oCat->favfood echo('</pre>'); ?> Outputs: ======== Bag Object ( [colour] => brown [oCat] => Cat Object ( [favfood] => FISH! ) ) Notice: Undefined property: Planet::$oBag->colour in test.php on line 36 Notice: Undefined property: Planet::$oBag->oCat in test.php on line 37 Notice: Undefined property: Planet::$oBag->oCat->favfood in test.php on line 38 SOLUTION So, I wrote a function that can handle this. The following code works fine: Example (works as expected): <?php class Cat { public $favfood = 'FISH!'; } class Bag { public $colour = 'brown', $oCat = null; function __construct () { $this->oCat = new Cat(); } } class Planet { public $oBag = null; function __construct() { $this->oBag = new Bag(); } } function mutlilevel_vv($variable_variable, &$subject, $limit=10, $level_syntax='->') { # allows infinite object depth access using a variable # does the variable variable even qualify for multi-level treatment? if(strpos($variable_variable,$level_syntax) !== false) { # one or more level syntax parts exist # split levels into array $object_levels = split($level_syntax,$variable_variable); # get initial reference to base level $cur_level = $subject->{$object_levels[0]}; # get total number of levels $num_object_levels = (count($object_levels)); # loop through all levels (skipping first one as we already did that) for($i=1;(($i < $num_object_levels) && ($i <= $limit));$i++) { $cur_level = $cur_level->{$object_levels[$i]}; } # return the target (mixed) return $cur_level; } else { # no multi-level processing required return $subject->{$variable_variable}; } } $oPlanet = new Planet(); $vv1 = 'oBag'; $vv2 = 'oBag->colour'; $vv3 = 'oBag->oCat'; $vv4 = 'oBag->oCat->favfood'; echo('<pre>'); print_r(mutlilevel_vv($vv1,$oPlanet)); # outputs Bag Object print_r(mutlilevel_vv($vv2,$oPlanet)); # ouputs 'brown' print_r(mutlilevel_vv($vv3,$oPlanet)); # outputs Cat Object print_r(mutlilevel_vv($vv4,$oPlanet)); # outputs 'FISH!' echo('</pre>'); ?> Outputs: ======== Bag Object ( [colour] => brown [oCat] => Cat Object ( [favfood] => FISH! ) ) brownCat Object ( [favfood] => FISH! ) FISH! ---- Server IP: 92.48.74.199 Probable Submitter: 91.143.69.131 ---- Manual Page -- http://www.php.net/manual/en/language.variables.variable.php Edit -- https://master.php.net/note/edit/82886 Del: integrated -- https://master.php.net/note/delete/82886/integrated Del: useless -- https://master.php.net/note/delete/82886/useless Del: bad code -- https://master.php.net/note/delete/82886/bad+code Del: spam -- https://master.php.net/note/delete/82886/spam Del: non-english -- https://master.php.net/note/delete/82886/non-english Del: in docs -- https://master.php.net/note/delete/82886/in+docs Del: other reasons-- https://master.php.net/note/delete/82886 Reject -- https://master.php.net/note/reject/82886 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138936) next »