note 123449 added to language.types.callable
| From: | chris dot rutledge at gmail dot com | Date: | Tue, 18 Dec 2018 10:12:49 +0000 |
| Subject: | note 123449 added to language.types.callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209918@lists.php.net to get a copy of this message | ||
Having read this line in the manual above,
"A method of an instantiated object is passed as an array containing an object at index 0 and
the method name at index 1. Accessing protected and private methods from within a class is
allowed."
I decided to do some testing to see if I could access private methods using the call_user_func
methods. Thankfully not, but for completeness here is my test which also covers using static and
object contexts
<?php
class foo {
public static $isInstance = false;
public function __construct() {
self::$isInstance = true;
}
public function bar() {
var_dump(self::$isInstance);
echo __METHOD__;
}
private function baz() {
var_dump(self::$isInstance);
echo __METHOD__;
}
public function qux() {
$this->baz();
}
public function quux() {
self::baz();
}
}
call_user_func(['foo','bar']); //fase, foo:bar
call_user_func(['foo','baz']); //warning, cannot access private method
call_user_func(['foo','quux']); //false, foo::baz
call_user_func(['foo','qux']); //fatal, Using $this when not in object context
$foo = new foo;
call_user_func([$foo,'bar']); //true, foo::bar
call_user_func([$foo,'baz']); //warning, cannot access private method
call_user_func([$foo,'qux']); //true, foo::baz
call_user_func(['foo','bar']); //true, foo::bar (static call, yet $isInstance
is true)
?>
----
Server IP: 208.43.231.9
Probable Submitter: 194.73.249.148
----
Manual Page -- http://php.net/manual/en/language.types.callable.php
Edit -- https://master.php.net/note/edit/123449
Del: integrated -- https://master.php.net/note/delete/123449/integrated
Del: useless -- https://master.php.net/note/delete/123449/useless
Del: bad code -- https://master.php.net/note/delete/123449/bad+code
Del: spam -- https://master.php.net/note/delete/123449/spam
Del: non-english -- https://master.php.net/note/delete/123449/non-english
Del: in docs -- https://master.php.net/note/delete/123449/in+docs
Del: other reasons-- https://master.php.net/note/delete/123449
Reject -- https://master.php.net/note/reject/123449
Search -- https://master.php.net/manage/user-notes.php