indirect function calls and variable variables

Dave Benjamin ramen at lackingtalent.com
Mon May 26 02:46:00 EDT 2003


In article <vcmt074rchvb4d at corp.supernews.com>, Cameron Laird wrote:
> PHPers do indeed use variable variables often--too much,
> in fact.  It's a general principle that languages which
> facilitate variable variables also are likely to offer
> a dictionary or associative array, and most uses of
> variable variables would better be recoded in terms of
> dictionaries.  There are PHP folk who know this already.
> If there's a question about it, someone invite me to 
> compl.lang.php, and I'll elaborate.

The reason the "variable variable" phenomenon (actually "call-by-name") pops
up so much in PHP is due to its lack of support for functions as values.
However, since PHP has a single, global namespace for functions, this ends
up being an effective way to write a dispatch, and safer than an eval().

> Erik illustrated the pattern with his function invocation
> above.  He didn't write there that such usage generally
> invites consideration for refactoring into an object-
> oriented polymorphism scheme.

I find that dispatching with an eval or dictionary lookup is often simpler
and more concise than using subtype inheritance with polymorphism. YMMV.

Dave




More information about the Python-list mailing list