note 24296 modified in language.variables.variable by danbrown
| From: | danbrown@php.net | Date: | Wed, 24 Jun 2009 17:00:45 +0000 |
| Subject: | note 24296 modified in language.variables.variable by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157415@lists.php.net to get a copy of this message | ||
Another use for this feature in PHP is dynamic parsing..
Due to the rather odd structure of an input string I am currently parsing, I must have a reference
for each particular object instantiation in the order which they were created. In addition, because
of the syntax of the input string, elements of the previous object creation are required for the
current one.
Normally, you won't need something this convolute. In this example, I needed to load an array
with dynamically named objects - (yes, this has some basic Object Oriented programming, please bare
with me..)
<?php
include("obj.class");
// this is only a skeletal example, of course.
$object_array = array();
// assume the $input array has tokens for parsing.
foreach ($input_array as $key=>$value){
// test to ensure the $value is what we need.
$obj = "obj".$key;
$$obj = new Obj($value, $other_var);
Array_Push($object_array, $$obj);
// etc..
}
?>
Now, we can use basic array manipulation to get these objects out in the particular order we need,
and the objects no longer are dependant on the previous ones.
I haven't fully tested the implimentation of the objects. The scope of a
variable-variable's object attributes (get all that?) is a little tough to crack. Regardless,
this is another example of the manner in which the var-vars can be used with precision where
tedious, extra hard-coding is the only alternative.
Then, we can easily pull everything back out again using a basic array function: foreach.
<?php
//...
foreach($array as $key=>$object){
echo $key." -- ".$object->print_fcn()." <br/>\n";
} // end foreach
?>
Through this, we can pull a dynamically named object out of the array it was stored in without
actually knowing its name.
--was--
Another use for this feature in PHP is dynamic parsing..
Due to the rather odd structure of an input string I am currently parsing, I must have a reference
for each particular object instantiation in the order which they were created. In addition, because
of the syntax of the input string, elements of the previous object creation are required for the
current one.
Normally, you won't need something this convolute. In this example, I needed to load an array
with dynamically named objects - (yes, this has some basic Object Oriented programming, please bare
with me..)
<?php
include("obj.class");
// this is only a skeletal example, of course.
$object_array = array();
// assume the $input array has tokens for parsing.
foreach ($input_array as $key=>$value){
// test to ensure the $value is what we need.
$obj = "obj".$key;
$$obj = new Obj($value, $other_var);
Array_Push($object_array, $$obj);
// etc..
}
?>
Now, we can use basic array manipulation to get these objects out in the particular order we need,
and the objects no longer are dependant on the previous ones.
I haven't fully tested the implimentation of the objects. The scope of a
variable-variable's object attributes (get all that?) is a little tough to crack. Regardless,
this is another example of the manner in which the var-vars can be used with precision where
tedious, extra hard-coding is the only alternative.
http://php.net/manual/en/language.variables.variable.php