NYCPHP Meetup

NYPHP.org

[nycphp-talk] follow-on variable variables question

Adam Fields adam at digitalpulp.com
Fri Feb 13 17:28:31 EST 2004


James Wetterau wrote:

> Based on my experience with Perl, I would have stayed away from
> variable variables, since the same tool in Perl (using a variable
> value as another variable's name) is Frowned Upon by Those in the
> Know.
> 
> Here's a cogent summary of the issues the practice raises in Perl:
> 
> http://perl.plover.com/varvarname.html
> 
> Anyone care to explain what, if anything, is different in PHP?

For starters, PHP doesn't let you shoot yourself in the foot quite as 
easily as perl does, with respect to special variables that you might 
accidentally overwrite.

I used this technique in cmacs, to allow code blocks that could be 
executed on a variety of different objects, where the objects themselves 
were dynamically named.

It's more complicated than this, but the essence is this:

$elementname = "element".$somelabel;
global $$elementname;
for ($count = 1; $count <= $vp[varcounter]; $varcount++) {
   $$elementname->add_subobject($vp[type], $vp[vname][$count], $vprop);
};

This is a small chunk of code that lets you set the name for an object 
according to a rule, then execute a method of that object. The array 
values passed to the method are also dynamically named.

I can't claim that this is a good idea, but it worked for me.

It's actually a classic TCL technique, which is where I learned it.




More information about the talk mailing list