note 98365 added to function.is-callable
| From: | ConstantinKpplinger at osu1 dot php dot net | Date: | Thu, 10 Jun 2010 19:16:28 +0000 |
| Subject: | note 98365 added to function.is-callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170037@lists.php.net to get a copy of this message | ||
It seems the only issue left with is_callable() is the disable_functions ini-setting. Apart from
that, is_callable() will reliably evaluate whether the passed function or method can be called from
within the same context is_callable() was called from, taking visibility and inheritance into
account. This includes functions, regular and static methods, magic functions and methods and
implemented interfaces (which are regular methods within the implementing class anyway).
<?php
function regular_function() {}
abstract class ParentTest
{
public function public_parent_method() {}
protected function protected_parent_method() {}
public static function static_method() {}
protected static function protected_static_method() {}
}
class CallableTest extends ParentTest implements Countable
{
public function __invoke() { } // Introduced in 5.3, see http://php.net/manual/language.oop5.magic.php
protected function protected_method() { }
public function is_callable($args)
{
return is_callable($args);
}
// Countable
public function count()
{
return 1;
}
}
$o = new CallableTest();
// Regular function:
var_dump(is_callable('regular_function')); // true
// Magic __invoke method:
var_dump(is_callable($o)); // true if PHP >= 5.3, false otherwise
// Countable implementation (regular method really):
var_dump(is_callable(array($o, 'count'))); // true
// Protected method from outside the object's scope:
var_dump(is_callable(array($o, 'protected_method'))); // false
// Protected method from inside the object's scope via public proxy method:
var_dump($o->is_callable(array($o, 'protected_method'))); // true
// Parent's public method
var_dump(is_callable(array($o, 'public_parent_method'))); // true
// Parent's protected method
var_dump(is_callable(array($o, 'protected_parent_method'))); // false
// Parent's protected method via proxy
var_dump($o->is_callable(array($o, 'protected_parent_method'))); // true
// Parent's static public method
var_dump(is_callable('CallableTest::static_method')); // true
// Parent's static protected method
var_dump(is_callable('CallableTest::protected_static_method')); // false
// Parent's static protected method via proxy
var_dump($o->is_callable('CallableTest::protected_static_method')); // true
?>
Tested PHP versions were 5.2.9 on openSUSE 10.3 (x64) and 5.3.1 on Windows Server 2003 (x86).
----
Server IP: 69.147.83.197
Probable Submitter: 85.127.111.98
----
Manual Page -- http://www.php.net/manual/en/function.is-callable.php
Edit -- https://master.php.net/note/edit/98365
Del: integrated -- https://master.php.net/note/delete/98365/integrated
Del: useless -- https://master.php.net/note/delete/98365/useless
Del: bad code -- https://master.php.net/note/delete/98365/bad+code
Del: spam -- https://master.php.net/note/delete/98365/spam
Del: non-english -- https://master.php.net/note/delete/98365/non-english
Del: in docs -- https://master.php.net/note/delete/98365/in+docs
Del: other reasons-- https://master.php.net/note/delete/98365
Reject -- https://master.php.net/note/reject/98365
Search -- https://master.php.net/manage/user-notes.php