note 109835 deleted from language.oop5.magic by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 15:42:05 +0000 |
| Subject: | note 109835 deleted from language.oop5.magic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190321@lists.php.net to get a copy of this message | ||
Note Submitter: cHao
----
osbertv at yahoo dot com is semi right; PHP syntax (at least in 5.3) doesn't allow you to call
$obj->someProperty(). When PHP sees that, it expects to find a *method* named someProperty.
However, there are at least two ways to accomplish this.
<?php
class A {
public function __invoke() { print "In A::__invoke()\n"; }
}
class B {
public $a;
public $closure;
public function __construct() {
$this->a = new A;
$this->closure = function() { print "In closure\n"; }
}
}
$b = new B;
$b->a->__invoke(); // works
call_user_func($b->a); // works too
// even works with closures
$b->closure->__invoke(); // works
call_user_func($b->closure); // works too
?>
call_user_func is probably better if you expect your invokables may be of the old ($object,
$methodName) or "$functionName" types.