note 83315 added to function.is-callable
| From: | RafaelM dot Salvioni at osu1 dot php dot net | Date: | Tue, 20 May 2008 16:10:24 +0000 |
| Subject: | note 83315 added to function.is-callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139559@lists.php.net to get a copy of this message | ||
The PHP's function is_callable not verify the visibility of the tested method.
The following function uses the Reflection classes of the PHP5 to check it.
<?php
/**
* Function is_callback().
*
* @param mixed $var Var
* @return bool
*/
function is_callback($var)
{
if (is_array($var) && count($var) == 2) {
$var = array_values($var);
if ((!is_string($var[0]) && !is_object($var[0])) || (is_string($var[0]) &&
!class_exists($var[0]))) {
return false;
}
$isObj = is_object($var[0]);
$class = new ReflectionClass($isObj ? get_class($var[0]) : $var[0]);
if ($class->isAbstract()) {
return false;
}
try {
$method = $class->getMethod($var[1]);
if (!$method->isPublic() || $method->isAbstract()) {
return false;
}
if (!$isObj && !$method->isStatic()) {
return false;
}
} catch (ReflectionException $e) {
return false;
}
return true;
} elseif (is_string($var) && function_exists($var)) {
return true;
}
return false;
}
?>
----
Server IP: 200.185.109.13
Probable Submitter: 200.162.225.69
----
Manual Page -- http://www.php.net/manual/en/function.is-callable.php
Edit -- https://master.php.net/note/edit/83315
Del: integrated -- https://master.php.net/note/delete/83315/integrated
Del: useless -- https://master.php.net/note/delete/83315/useless
Del: bad code -- https://master.php.net/note/delete/83315/bad+code
Del: spam -- https://master.php.net/note/delete/83315/spam
Del: non-english -- https://master.php.net/note/delete/83315/non-english
Del: in docs -- https://master.php.net/note/delete/83315/in+docs
Del: other reasons-- https://master.php.net/note/delete/83315
Reject -- https://master.php.net/note/reject/83315
Search -- https://master.php.net/manage/user-notes.php