tags:

views:

70

answers:

2

Original Question

Since a recent version of php added anonymous functions, is there a way to extend functions? in Javascript I'd do:

var temp = immaFunction;
immaFunction = function(){
  //do some random stuff
  temp.apply(this, arguments);
}

Result

As of 5.3, PHP has first class anonymous functions.
A few points to consider however(will be filling this with more as I mess around with it.):

  • You must import any external variables you want to use. (Example 1)

Examples

Example 1:

$foo = "bar";
$fooBar = function() use ($foo){
  echo $foo;
}
$fooBar(); //bar
A: 

I believe PHP also has functions as first-class objects. http://en.wikipedia.org/wiki/PHP#Functions

Iznogood is right.

AJ
+2  A: 

PHP 5.3 supports that link

Iznogood
It does support the same syntax, thanks
Rixius
One problem if you copy an existing function, it var_dump's to a string of the functions name.
Rixius
It does not support the same syntax exactly. To use local variables, you need to add the `use` clause: `$func = function() use ($temp) { $temp->something(); }`. Unlike JS, the functions do not inherit the local scope by default (You need to "bind" them in using the `use` clause to retain access)...
ircmaxell
@ircmaxell Thanks for that
Rixius