note 119218 added to function.is-callable
| From: | heahatheahproddotcom | Date: | Wed, 20 Apr 2016 23:48:50 +0000 |
| Subject: | note 119218 added to function.is-callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205291@lists.php.net to get a copy of this message | ||
I don't know if it is a bug but when you test an array with a class and method, is_callable
returns true for non static method.
Consider the following code:
<?php
class A
{
public static function test()
{
echo 'test', '<br>';
}
public function hello()
{
echo 'hello', '<br>';
}
}
echo "Static #1: call_user_func(array('A', 'test')) => ",
call_user_func(array('A', 'test')), '<br>';
echo 'expect is_callable TRUE => ',
is_callable(array('A', 'test'))
? 'TRUE, A::test() is callable statically'
: 'FALSE, A::test() is not callable statically',
'<br><br>'
;
echo "Static #2: call_user_func(array('A', 'hello')) => ",
call_user_func(array('A', 'hello')), '<br>';
echo 'expect is_callable FALSE => ',
is_callable(array('A', 'hello'))
? 'TRUE, A::hello() is callable statically'
: 'FALSE, A::hello() is not callable statically',
'<br><br>'
;
$a = new A();
echo "Instance #1: call_user_func(array(\$a, 'test')) => ",
call_user_func(array($a, 'test')), '<br>';
echo 'expect is_callable TRUE => ',
is_callable(array($a, 'test'))
? 'TRUE, $a::test() is callable'
: 'FALSE, $a::test() is not callable',
'<br><br>'
;
echo "Instance #2: call_user_func(array(\$a, 'hello')) => ",
call_user_func(array($a, 'hello')), '<br>';
echo 'expect is_callable FALSE => ',
is_callable(array($a, 'hello'))
? 'TRUE, $a::hello() is callable'
: 'FALSE, $a::hello() is not callable',
'<br><br>'
;
?>
Will output:
Static #1: call_user_func(array('A', 'test')) => test
expect is_callable TRUE => TRUE, A::test() is callable statically
Static #2: call_user_func(array('A', 'hello')) =>
Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method
A::hello() should not be called statically in test.php on line 24
hello
expect is_callable FALSE => TRUE, A::hello() is callable statically
Instance #1: call_user_func(array($a, 'test')) => test
expect is_callable TRUE => TRUE, $a::test() is callable
Instance #2: call_user_func(array($a, 'hello')) => hello
expect is_callable TRUE => TRUE, $a::hello() is callable
------------------------------------------------------------
Also note that this works the same for inherited methods (I read other posts suggesting otherwise),
and that private methods (static, inherited or both) always return false in any case as expected.
Tested with php 5.6.20.
----
Server IP: 72.52.91.14
Probable Submitter: 80.12.55.80
----
Manual Page -- http://php.net/manual/en/function.is-callable.php
Edit -- https://master.php.net/note/edit/119218
Del: integrated -- https://master.php.net/note/delete/119218/integrated
Del: useless -- https://master.php.net/note/delete/119218/useless
Del: bad code -- https://master.php.net/note/delete/119218/bad+code
Del: spam -- https://master.php.net/note/delete/119218/spam
Del: non-english -- https://master.php.net/note/delete/119218/non-english
Del: in docs -- https://master.php.net/note/delete/119218/in+docs
Del: other reasons-- https://master.php.net/note/delete/119218
Reject -- https://master.php.net/note/reject/119218
Search -- https://master.php.net/manage/user-notes.php