Re: Closures in PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux