Lambda Functions in PHP aren't Logical
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.
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).
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.
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
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.
Reference: Anonymous functions
The OP has already presented the solution:
$foo = new stdClass;
$foo->bar = function(){ echo "bar"; };
$test = $foo->bar;
$test();
I.e., any property that contains an anon function has an inherent ambiguity because adding parenthesis after the property name tells PHP that you are calling a method, and not invoking an anon function in a property. To resolve this ambiguity, you must add a degree of separation by storing the property into a local variable first, and then invoking the anon function.
You just have to look at the class property as a property instead of as "a property callable as a method" no matter what it's contents are and assume that php is going to look for a real class method if you put parenthesis after the property.
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.
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.
精彩评论