Lambda Functions in PHP aren't Logical
- by Chacha102
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.