|
 |
is_callable (PHP 4 >= 4.0.6, PHP 5) is_callable --
Verify that the contents of a variable can be called as a function
Descriptionbool is_callable ( mixed var [, bool syntax_only [, string &callable_name]] )
Verify that the contents of a variable can be called as a function.
This can check that a simple variable contains the name of a valid
function, or that an array contains a properly encoded object and
function name.
The var parameter can be either the name of a function stored in
a string variable, or an object and the name of a method within the
object, like this:
array($SomeObject, 'MethodName') |
If the syntax_only argument is TRUE the
function only verifies that var might be a
function or method. It will only reject simple variables that are
not strings, or an array that does not have a valid structure to be
used as a callback. The valid ones are supposed to have only 2
entries, the first of which is an object or a string, and the
second a string.
The callable_name argument receives the
"callable name". In the example below it's
"someClass:someMethod". Note, however, that despite the
implication that someClass::SomeMethod() is a callable static
method, this is not the case.
See also function_exists() and
method_exists().
is_callable
jphp at dsf dot org dot uk
12-Feb-2006 03:38
bob at theshirdshift:
"function_exists" doesn't do this, no, but "method_exists" works fine, and is still faster than is_callable:
<?php
function doTimes($start, $end)
{
$start_time = explode (" ", $start);
$start_time = $start_time[1] + $start_time[0];
$end_time = explode (" ", $end);
$end_time = $end_time[1] + $end_time[0];
$time = $end_time - $start_time;
return $time;
}
class test
{
function test()
{
return true;
}
}
$test = new test;
$callableIsTrue = false;
$startIsCallable = microtime();
for($i = 0; $i < 10000; $i++)
{
if(is_callable(array('test', 'test'))) { $callableIsTrue = true; }
}
$endIsCallable = microtime();
$existsIsTrue = false;
$startExists = microtime();
for($i = 0; $i < 10000; $i++)
{
if(method_exists('test', 'test')) { $existsIsTrue = true; }
}
$endExists = microtime();
$timeIsCallable = doTimes($startIsCallable, $endIsCallable);
$timeExists = doTimes($startExists, $endExists);
echo "<b>is_callable = ".($callableIsTrue ? "TRUE" : "FALSE")."</b>, \n";
echo "<b>method_exists = ".($existsIsTrue ? "TRUE" : "FALSE")."</b><br>\n";
echo "<br>Did 10000 is_callables in ".$timeIsCallable." seconds";
echo "<br>Did 10000 method_exists in ".$timeExists." seconds";
?>
is_callable = TRUE, method_exists = TRUE
Did 10000 is_callables in 0.410346984863 seconds
Did 10000 method_exists in 0.175447940826 seconds
yetanotheruser at hotmail etc
15-Dec-2005 06:47
I have come across a strange oddity in versions around the 4.3.11 mark - I may have missunderstood the purpose of this function but hope this'll be helpful for some.
The point the code below is supposed to illustrate is that in some cases with
<? $myFunc = Array( $myObj, 'myMethod' ); ?>
<? is_callable( $myFunc, true, $callMe ); ?>
will return true, and give you $callMe set to myObj::myMethod but calling
<? $callMe(); ?>
doesn't work... however calling
<? $myFunc[0]->$myFunc[1](); ?>
seems to work fine..
... the reason all the code is down there is I think this oddity is due to how/the order in which I've instantiated my classes or something...
anyhow... HTH someone! :-)
Code follows:
FILE 1 :
<?
include('myTools');
$foo = new myClass();
print $foo->getMySource();
class myClass{
var $flibble = 'wibble';
function myClass(
$this->tools = new myTools();
)
function getMySource(){
$args = $this->flibble;
$source = $this->tools->returnSource( Array ( $this, 'someHTML' ), $args );
return ( $source );
}
function someHTML($args){
?>
Here is some HTML.. that we want to build outside a PHP block,
possibly just cos it's tidier in <B>your favourite text editor</B>..
.. or you want this function to be elsewhere.. for your designers
to play with etc.. ... incidentally I'd like to say "<?=$args?>" etc.
<?
}
}
?>
FILE 2:
<?
class myTools(){
function returnSource($func, $args){
if ( is_callable ( $func, true, $callMe ){
ob_start();
if ( is_array($func) ){
$func[0]->$func[1]($args);
} else {
$func($args);
}
$rtn = ob_get_contents();
ob_clean();
return ( $rtn );
} else {
error_log("Doh!");
}
}
}
?>
empyone at tiscalinet dot it
08-Nov-2005 12:41
To corey at eyewantmedia dot com:
your misunderstanding lies in passing in the naked $object parameter. It is correct for is_callable to return FALSE since you cannot 'call an object', you can only call one of its methods, but you don't specify which one. Hence:
is_callable(array($object, 'some_function'), [true or false], $callable_name)
will yield the correct result.
Notice, though, that a quick test I made (PHP 5.0.4) showed that is_callable incorrectly returns TRUE also if you specify the name of a protected/private method from outside of the context of the defining class, so, as wasti dot redl at gmx dot net pointed out, reflection is the way to go if you want to take visibility into account (which you should for true OOP, IMHO).
wasti dot redl at gmx dot net
09-Oct-2005 04:14
The way to discover whether a method exists in face of a __call is reflection.
It should be mentioned that although array('Test', 'func') is callable according to this function (where func is a public static method of Test), actually calling this construct as $fn() fails.
corey at eyewantmedia dot com
18-Mar-2005 03:39
I've been spending a month on and off trying to figure out why
is_callable($object, [true or false], $varContainingFunctionName)
returned false when it should not have (ie: $object->FunctionName() was callable), I realized I must have misunderstood its purpose. If you find yourself in the same situation, try
function_exists(string functionname)
or
method_exists ( object object, string method_name )
before you rip your hair out :)
mcroghan at digitalkeg dot com
10-Feb-2005 07:36
Be careful when using this function and __call (PHP5). This function will always report true when using __call.
Need a specific function for the purpose of checking if a class method exists explicitly even when using __call.
Haven't ruled out the possibility of the existence of such a function yet. So if someone knows of one, please point it out.
bob at thethirdshift dot net
23-Jun-2004 09:54
I, too, was wondering whether is_callable or function exists is faster when checking class methods. So, I setup the following test:
<?php
function doTimes($start, $end)
{
$start_time = explode (" ", $start);
$start_time = $start_time[1] + $start_time[0];
$end_time = explode (" ", $end);
$end_time = $end_time[1] + $end_time[0];
$time = $end_time - $start_time;
return $time;
}
class test
{
function test()
{
return true;
}
}
$callableIsTrue = false;
$startIsCallable = microtime();
for($i = 0; $i < 10000; $i++)
{
if(is_callable(array('test', 'test'))) { $callableIsTrue = true; }
}
$endIsCallable = microtime();
$existsIsTrue = false;
$startExists = microtime();
for($i = 0; $i < 10000; $i++)
{
if(function_exists('test::test')) { $existsIsTrue = true; }
}
$endExists = microtime();
$timeIsCallable = doTimes($startIsCallable, $endIsCallable);
$timeExists = doTimes($startExists, $endExists);
echo "<b>is_callable = ".($callableIsTrue ? "TRUE" : "FALSE")."</b>, \n";
echo "<b>function_exists = ".($existsIsTrue ? "TRUE" : "FALSE")."</b><br>\n";
echo "<br>Did 10000 is_callables in ".$timeIsCallable." seconds";
echo "<br>Did 10000 function_exists in ".$timeExists." seconds";
?>
This gives the output :
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0640790462494 seconds
Did 10000 function_exists in 0.0304429531097 seconds
So the fact that function_exists is twice as fast is slightly over shadowed by the fact that it doesn't work on class methods, at least not as far as I can tell.
webmaster __AT__ digitalanime __DOT__ nl
03-Apr-2004 02:30
<?php
while(list($key,$value)=each($HTTP_POST_VARS))
{
$tmpVar = 'return isset($' . 'this->' . $key . ');';
if(is_callable($key) && eval($tmpVar) && trim($value) != "")
{
$tmpSet = '$this->set' . ucfirst($key) . "('" . $value . "');";
eval($tmpSet);
}
}
?>
Why do you use this?
Isn't this a better solvation (or.. Whatever :P)
<?php
foreach($_POST as $key => $value)
{
if(is_callable($key) && isset($this->{$key}) && trim($value != '')
{
$this->{'set' . ucfirst($key)}($value);
}
}
?>
Tada.. Variable objects, that's what they are..
| |