Re: Closures in PHP
| From: | Nathan Nobbe | Date: | Tue, 15 Jan 2008 03:58:43 +0000 |
| Subject: | Re: Closures in PHP | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-267272@lists.php.net to get a copy of this message | ||
when it comes to create_function(), id say its just as painful as building
functions with html or writing queries by hand. namely, its prone to a lot
of string escaping which produces awful hard to read code. i mean, the
kind of code you write yourself and then look at a week later and say
'what a mess'...
anyway, it occurs to me that the closest thing php has to functional
languages,
thats readily usable is the variable function mechanism
http://www.php.net/manual/en/functions.variable-functions.php
theres been discussion about it on the list in the past, though often it is
spoken of as a ghost feature. its quite nice.
as an example, suppose you have a string
$blah = 'myFunc';
then if myFunc is defined as a function, it can be invoked per
$blah(/* params */);
it creates the ability to do all sorts of powerful things, such as
delegation
whereby switch statements can be eliminated, callbacks, whereby a formal
parameter could be a string that refers to a method name, and they work
on object references as well. so you can get away w/
$this->$dynamicFuncName(/* params*/);
in some sense you are passing around a function, but in reality there is
no preservation of context at all. whats worse, the function that gets
invoked
had better be loaded into the interpreter or youll encounter a runtime
error.
but i do enjoy this feature.
honestly im still struggling with closures as i continue to grow my skills
with javascript, but at least the concepts are starting to sink in.
being able to create anonymous functions on the fly would be a welcome
feature in my book; and hey, how about anonymous objects while were at it ;)
-nathan