3

Possible Duplicate:
Initialize class property with an anonymous function

I've been programing PHP for quite a while, and PHP 5.3 anonymous functions are one of those thinks that help you out a lot while building some simple scripts. However, I cannot understand why would the following example won't work?

$db         = new PDO([..]);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->die    = function($str){ die(var_dump( $str )); };

$db->die('[..]');

After all, all I do is declare anonymous function on ->die property of PDO instance. This kinda makes me think this is a PHP bug.

Community
  • 1
  • 1
Gajus
  • 69,002
  • 70
  • 275
  • 438

3 Answers3

1

Assigning a function to a property does not change the property into a function. To execute a function stored within a property you must use the __call Magic Method:

class Foo extends PDO {
    function __call($function, $args) {
        return call_user_func_array($this->{$function}, $args);
    }
}

$db         = new Foo([..]);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->die    = function($str){ die(var_dump( $str )); };

$db->die('[..]');
Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55
0

According to the answers to this question: php Set a anonymous function in an instance

It is by design (or a design error). There are some workaround suggestions provided there.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • That's a different case. I declare the anonymous function after the instance is initiated. There is no obvious reason for it to fail. – Gajus Jun 26 '11 at 12:08
  • @Guy ah, true. The answers in the second link should apply in any case though? – Pekka Jun 26 '11 at 12:10
  • Are you referring to `__call()` "workaround"? That requires extending the original class and generally brakes the concept of anonymous function. – Gajus Jun 26 '11 at 12:12
  • @Guy no, I mean the `call_user_func_array()` workaround that should work regardless of whether you extend the original class or not. It's far from elegant though. This can be viewed as a design flaw, but I don't think there is anything that can be done (except complaining to PHP developers about this, and asking that it be fixed in a future version) – Pekka Jun 26 '11 at 12:13
  • Anyway, I will keep the answer alive for few more hours, to see if anyone stands in with a more reasoned explanation. – Gajus Jun 26 '11 at 12:16
  • @Guy the explanation is probably in PHP's architecture. Even though anonymous functions are possible now in PHP, functions are not first class citizens in PHP as they are e.g. in JavaScript. I guess to be directly callable, a method still has to be registered in the class definition. Anyway, interested to see whether anybody can come up with evidence – Pekka Jun 26 '11 at 12:20
0

This works:

class Foo{
    public $bar;
}
$foo = new Foo;
$foo->bar = function(){
    echo "Hello, world\n";
};
call_user_func($foo->bar);
Álvaro González
  • 142,137
  • 41
  • 261
  • 360