Lambda Functions in PHP aren't Logical

Posted by Chacha102 on Stack Overflow See other posts from Stack Overflow or by Chacha102
Published on 2010-01-17T07:03:29Z Indexed on 2010/06/03 10:34 UTC
Read the original article Hit count: 238

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about oop