note 82886 deleted from language.variables.variable by felipe

From: Date: Thu, 01 May 2008 03:04:41 +0000
Subject: note 82886 deleted from language.variables.variable by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-138960@lists.php.net to get a copy of this message
Note Submitter: Matthew Hall ---- 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!

« previous php.notes (#138960) next »