tags:

views:

339

answers:

6

Note: I have condensed this article into my person wiki: http://wiki.chacha102.com/Lambda - Enjoy

I am having some troubles with Lambda style functions in PHP.

First, This Works:

$foo = function(){ echo "bar"; };
$foo();

Second, This Works:

class Bar{
    public function foo(){
       echo "Bar";
    }

Third, This works:

$foo = new stdClass;
$foo->bar = function(){ echo "bar"; };
$test = $foo->bar;
$test();

But, this does not work:

$foo = new stdClass;
$foo->bar = function(){ echo "bar"; };
$foo->bar();

And, this does not work

class Bar{
    public function foo(){
       echo "Bar";
    }
$foo = new Bar;
$foo->foo = function(){ echo "foo"; };
$foo->foo(); // echo's bar instead of Foo.

My Question is Why?, and how can I assure that both this:

$foo->bar = function(){ echo "test"; };
$foo->bar();

and this

$foo = new Bar;
$foo->bar();

are called properly? Extra Points if you can point to documentation stating why this problem occurs.

+3  A: 

Functions and methods are treated differently in PHP. You can use runkit_method_add() to add a method to a class, but I know of no way to add a method to an instance.

Ignacio Vazquez-Abrams
It just seems weird. If there isn't a method by the name of `bar`, it should look for variable `bar` and see if it is callable, so this line `$foo->bar()` works.
Chacha102
You aren't necessarily wrong. But the Zend engine isn't built that way.
Ignacio Vazquez-Abrams
+1  A: 

The closest you could get to make this happen would be by using the __call overload to check if a property contains a closure:

class what {
  function __call($name, $args) {
    $f= $this->$name;
    if ($f instanceof Closure) {
      $f();
    }
  }
}

$foo = new what();
$foo->bar = function(){ echo "bar"; };
$foo->bar();

Though bear in mind the following note from the docs:

Anonymous functions are currently implemented using the Closure class. This is an implementation detail and should not be relied upon.

http://us3.php.net/manual/de/functions.anonymous.php

pygorex1
Another option would to use the `is_callable` statement. I'm not sure if that will tell me if it is a closure though.
Chacha102
This still won't quite work - if there's a method declaration for `foo()` the `__call()` won't be called.
pygorex1
I'm not too concerned about overriding a method. I'll come back in the morning, this code might work. Would just have to create a class like `stdCallableObj` that would act as a `stdClass` when I want to assign closures to it.
Chacha102
+7  A: 

This is an interesting question. This works:

$a = new stdClass;
$a->foo = function() { echo "bar"; };
$b = $a->foo;
$b(); // echos bars

but as you say this doesn't:

$a = new stdClass;
$a->foo = function() { echo "bar"; };
$a->foo();

If you want an object to which you can dynamically call members, try:

class A {
  public function __call($func, $args) {
    $f = $this->$func;
    if (is_callable($f)) {
      return call_user_func_array($f, $args);
    }
  }
}

$a = new A;
$a->foo = function() { echo "bar\n"; };
$a->foo2 = function($args) { print_r($args); };
$a->foo();
$a->foo2(array(1 => 2, 3 => 4));

But you can't replace callable methods this way because __call() is only called for methods that either don't exist or aren't accessible (eg they're private).

cletus
This along with other comments let me set up a `stdObj` class that I can use to reliably create a object without a class (kind of). Thanks!
Chacha102
+1  A: 

Interesting question although I see no reason why this should work:

class Bar{
    public function foo(){
       echo "Bar";
    }
$foo = new Bar;
$foo->foo = function(){ echo "foo"; };
$foo->foo(); // echo's bar instead of Foo.

I had a similar problem with __invoke(), and I've also not been able to solve it:

class base {
    function __construct() {
        $this->sub = new sub();
    }

    function __call($m, $a) {
    }
}

class sub {
    function __invoke($a) {
    }
}

$b = new base();
$b->sub('test'); // should trigger base::__call('sub', 'test') or sub::__invoke('test')?

Solution? Never use __invoke()! :P

Alix Axel
A: 

To me it seems like a bug rather than a "quirk":

<?php
$t = new stdClass;
$t->a = function() { echo "123"; };
var_dump($t);
echo "<br><br>";
$x = (array)$t;
var_dump($x);
echo "<br><br>";
$x["a"]();
echo "<br><br>";
?>

object(stdClass)#1 (1) { ["a"]=> object(Closure)#2 (0) { } } 

array(1) { ["a"]=> object(Closure)#2 (0) { } } 

123

It is there, I just don't think that PHP knows how to run it.

Nathan Adams
A: 

Consider this:

<?php

class A {

    public $foo

    public function foo() {
        return 'The method foo';
    }

}

$obj = new A;
$obj->foo = function() { return 'The closure foo'; };

var_dump($obj->foo());

Do you mean $obj->foo() the closure or $obj->foo() the method? It is ambiguous and PHP makes the decision that you mean the method. To use the closure, you have to disambiguate what you mean. One way you can do this is by using a temporary variable as you have done. Another way is to use call_user_func($obj->foo).

I do not know of any other easy way.

erisco