note 116640 added to language.types.callable
| From: | edanschwartz at gmail dot com | Date: | Mon, 02 Feb 2015 18:13:50 +0000 |
| Subject: | note 116640 added to language.types.callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201581@lists.php.net to get a copy of this message | ||
You can use 'self::methodName' as a callable, but this is dangerous. Consider this
example:
<?php
class Foo {
public static function doAwesomeThings() {
FunctionCaller::callIt('self::someAwesomeMethod');
}
public static function someAwesomeMethod() {
// fantastic code goes here.
}
}
class FunctionCaller {
public static function callIt(callable $func) {
call_user_func($func);
}
}
Foo::doAwesomeThings();
?>
This results in an error:
Warning: class 'FunctionCaller' does not have a method 'someAwesomeMethod'.
For this reason you should always use the full class name:
<?php
FunctionCaller::callIt('Foo::someAwesomeMethod');
?>
I believe this is because there is no way for FunctionCaller to know that the string
'self' at one point referred to to
Foo.
----
Server IP: 72.52.91.14
Probable Submitter: 2601:2:5180:f800:1d3d:f274:18bf:6a07
----
Manual Page -- http://php.net/manual/en/language.types.callable.php
Edit -- https://master.php.net/note/edit/116640
Del: integrated -- https://master.php.net/note/delete/116640/integrated
Del: useless -- https://master.php.net/note/delete/116640/useless
Del: bad code -- https://master.php.net/note/delete/116640/bad+code
Del: spam -- https://master.php.net/note/delete/116640/spam
Del: non-english -- https://master.php.net/note/delete/116640/non-english
Del: in docs -- https://master.php.net/note/delete/116640/in+docs
Del: other reasons-- https://master.php.net/note/delete/116640
Reject -- https://master.php.net/note/reject/116640
Search -- https://master.php.net/manage/user-notes.php