Home » Php » class – php Set a anonymous function in an instance

class – php Set a anonymous function in an instance

Posted by: admin July 12, 2020 Leave a comment


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');};

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!!


How to&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'); };



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.


# real ugly, but PoC...

class a {
  function __call($f, $x) {
    call_user_func_array($this->$f, $x);

$a = new a;
$a->b = function() { echo "Hello world"; };


Sounds like you are describing a Strategy Pattern or Decorator Pattern – there are other ways to achieve this in way which is more easily communicated with other developers who read your code.


You can do something along these lines (which will also work with callbacks that are not closures):

class A {
    private $fun;
    function setFun($fun) {
        if (!is_callable($fun))
            throw new InvalidArgumentException();
         $this->fun = $fun;
    public function fun() {
        call_user_func_array($this->fun, func_get_args());

$c = new A();

$c->setFun(function($a) { echo('HelloWorld ' . $a);});

which gives HelloWorld here.

That said, you should also consider inheritance or the decorator pattern.


This is not an issue anymore by PHP 7;

// no error
$result = ($this->anonFunc)();
$result = ($this->anonFunc)($arg1, $arg2, ...);

See more about AST.


Rather than hooking a __call magic method into your class, you can instead execute the callable directly using call_user_func.

class A {
    public $b;

$c = new A();
$c->b = function(){echo('HelloWorld');};

call_user_func($c->b);    // HelloWorld

Obviously it would be nice for PHP to provide some syntax to execute this directly.