Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php Set a anonymous function in an instance

I am just starting out with PHP, and I am wondering if there is a way to add an anonymous function to a class instance.

For instance, lets say...

class A{
    public B;
}

$c = new A();

//This is where I am getting a little confused...
//The following wont work

$c->B = function(){echo('HelloWorld');};
$c->B();

What I am hoping to do is reuse the same spit of code in a great number of different applications, and make it so that I can just 'swap-out' and replace functions in specific instances.

I am using php5.3 (so anonymous functions should work, just not in the way that I am using them).

Thanks so very much for your time!!

-GK

like image 388
geekay Avatar asked Jun 13 '10 21:06

geekay


People also ask

Does PHP have anonymous function?

An anonymous function is a function that doesn't have a name. Since the function doesn't have a name, you need to end it with a semicolon ( ; ) because PHP treats it as an expression. This anonymous function is not useful at all because you cannot use it like a named function.

Can we assign an anonymous function?

The () makes the anonymous function an expression that returns a function object. An anonymous function is not accessible after its initial creation. Therefore, you often need to assign it to a variable. In this example, the anonymous function has no name between the function keyword and parentheses () .

What Is syntax of anonymous function in PHP?

Syntax. $var=function ($arg1, $arg2) { return $val; }; There is no function name between the function keyword and the opening parenthesis. There is a semicolon after the function definition because anonymous function definitions are expressions.

How can you pass a local variable to an anonymous function in PHP?

Yes, use a closure: functionName($someArgument, function() use(&$variable) { $variable = "something"; }); Note that in order for you to be able to modify $variable and retrieve the modified value outside of the scope of the anonymous function, it must be referenced in the closure using & . It's new!


2 Answers

You can use the __call magic function for this job. Not a beauty, but it works..

like this:

class A {
    public $B;

    public function __call($closure, $args)
    {
        call_user_func_array($this->$closure, $args);
    }
}

$c = new A();

$c->B = function () { echo('HelloWorld'); };
$c->B();
like image 68
therufa Avatar answered Oct 18 '22 02:10

therufa


FWIW:

PHP 5.3's treatment of anonymous functions is entertaining. This won't work:

$c->B = function() { echo func_get_arg(0); };
$c->B("This fails :(");

This WILL work:

$c->B = function() { echo func_get_arg(0); };
$hilarious = $c->B;
$hilarious("This works!");

To work around this, you need to use a __call hack like the one provided by Oden.

This behavior may change in the future. The array dereferencing RFC was recently committed to PHP's trunk, and the patch has set off a discussion on function call chaining, the syntax of which may allow what you're trying to do without the __call hack. Unfortunately it's proven difficult in the past to get function call chaining working.

like image 36
Charles Avatar answered Oct 18 '22 03:10

Charles