3

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
0

2 Answers 2

3

PHP 5.3 supports that link

Sign up to request clarification or add additional context in comments.

2 Comments

One problem if you copy an existing function, it var_dump's to a string of the functions name.
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)...
0

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

Iznogood is right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.