note 109835 added to language.oop5.magic
| From: | cHao | Date: | Sat, 25 Aug 2012 18:46:02 +0000 |
| Subject: | note 109835 added to language.oop5.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190220@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 98.249.12.255
----
Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php
Edit -- https://master.php.net/note/edit/109835
Del: integrated -- https://master.php.net/note/delete/109835/integrated
Del: useless -- https://master.php.net/note/delete/109835/useless
Del: bad code -- https://master.php.net/note/delete/109835/bad+code
Del: spam -- https://master.php.net/note/delete/109835/spam
Del: non-english -- https://master.php.net/note/delete/109835/non-english
Del: in docs -- https://master.php.net/note/delete/109835/in+docs
Del: other reasons-- https://master.php.net/note/delete/109835
Reject -- https://master.php.net/note/reject/109835
Search -- https://master.php.net/manage/user-notes.php