views:

119

answers:

5

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

+1  A: 

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

<?php
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);});
$c->fun("here");

which gives HelloWorld here.

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

Artefacto
A: 

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.

Paul Dixon
+2  A: 
# 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"; };
$a->b();
+3  A: 

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();
Nort
gk Here, I will look into this... I will need to figure out what is does... Thanks so very much for pointing me in the right direction!
geekay
edited to revert the downvote. I saw you posted the exact same solution as user358390 (minus a minor difference), but I hadn't noticed the times were so close, so I'm giving the benefit of the doubt
Artefacto
+1  A: 

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.

Charles