downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

is_double> <is_buffer
Last updated: Fri, 24 Jul 2009

view this page in

is_callable

(PHP 4 >= 4.0.6, PHP 5)

is_callable 변수 내용을 함수처럼 호출할 수 있는지 확인

설명

bool is_callable ( mixed $var [, bool $syntax_only [, string &$callable_name ]] )

변수의 내용이 함수처럼 호출할 수 있는지 확인합니다. 변수가 유효한 함수의 이름을 포함하고 있는지, 혹은 인코드된 객체와 함수 이름을 가지는 배열인지를 간단히 확인할 수 있습니다.

인수

var

문자열 변수에 저장한 함수의 이름이거나, 객체 안의 객체와 메쏘드의 이름일 수 있습니다. 예:

array($SomeObject, 'MethodName')

syntax_only

TRUE로 설정하면, 이 함수는 var 가 함수나 메쏘드일 수 있는지만 확인합니다. 이는 단순히 문자열이 아닌 변수나 유효하게 콜백에 사용할 수 있는 구조가 아닌 배열을 거부합니다. 유효한 것은 2 엔트리뿐이며, 첫번째는 객체나 문자열, 두번째는 문자열입니다.

callable_name

"호출할 수 있는 이름"을 받습니다. 아래의 예제에서 이는 "someClass:someMethod"입니다. 그러나 somClass::SomeMethod()가 호출할 수 있는 정적 메쏘드라면, 불가능하다는 점에 주의하십시오.

반환값

var 를 호출할 수 있으면 TRUE, 아니면 FALSE를 반환합니다.

예제

<?php
//  변수를 함수처럼 호출할 수 있는지
//  체크하는 방법.

//
//  함수를 포함하는 간단한 변수
//

function someFunction()
{
}

$functionVariable 'someFunction';

var_dump(is_callable($functionVariablefalse$callable_name));  // bool(true)

echo $callable_name"\n";  // someFunction

//
//  메쏘드를 포함하는 배열
//

class someClass {

  function 
someMethod()
  {
  }

}

$anObject = new someClass();

$methodVariable = array($anObject'someMethod');

var_dump(is_callable($methodVariabletrue$callable_name));  //  bool(true)

echo $callable_name"\n";  //  someClass:someMethod

?>

참고



is_double> <is_buffer
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
is_callable
juand at softwarecubes dot com
22-Feb-2009 04:39
Revised function with static support:

<?php
   
/**
    * The is_callable php function only considers methods declared in the class itself, and ignores the parent's.
    * This version considers all of the hierarchy.
    *
    * @param (string|Object) $class_name
    * @param string $method_name
    * @param bool $static the method being tested is static.
    */
   
public static function isCallable( $class_name, $method_name, $static = false ){
       
        if( !
is_string( $class_name ) ){
           
$class_name = get_class( $class_name );
        }
       
       
// Define Callable
       
if( $static ){
           
$callable = "{$class_name}::{$method_name}";
        }else{
           
$callable = array( $class_name, $method_name );
        }
       
       
       
// Check class itself
           
if( @is_callable( $callable ) === true ){
                if(
$method_name == 'setEmailAddressTypeHash' ) {
                   
ErrorHandler::preDump($callable);
                }
                return
true;
            }
       
       
// Check all parents
           
while( $parent_class = get_parent_class( $class_name ) ){
                if( @
is_callable( $callable ) === true ){
                    return
true;
                }
               
$class_name = $parent_class;
            }
           
        return
false;
    }
?>
Rafael M. Salvioni
21-May-2008 01:10
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;
}

?>
rahadotaboulfethatgmail.com
02-Mar-2008 09:44
is_callable generates an [E_STRICT] error if the  tested method cannot be called staticly. (and returns the good value)

I used @is_called
i'm using php 5.2.1
crestfresh at gmail dot com
18-Feb-2008 02:23
Furthuring mw's note [is_callable correctly takes into account scope so that is_callable(array($obj, 'privateMethod'))  correctly returns false when called from outside $obj's scope]:

It works in 5.1.6 too.
Quis strrev TA omicidio strrev TOD com
14-Nov-2007 08:50
is_callable() does _not_ check wheter this function is disabled by php.ini's disable_functions

use:

<?PHP
function is_disabled($function) {
 
$disabled_functions=explode(',',ini_get('disable_functions'));
  return
in_array($function, $disabled_functions);
}
?>

I`m running PHP 5.2.4
mw at NO-SPAM hire mason wolf dot com
19-Oct-2007 11:35
As empyone noted, early versions of php 5 incorrectly returned true if is_callable checked on a protected or private method. Later versions of php 5 will now only return true if the method is public and can be called externally. I do not know precisely when this behavior was changed, so you may have to test on your own. But sometime between 5.0.4, which empyone said he was using, and 5.2.4 where I tested it myself, the behavior was modified.
cipri at php dot net
15-Jun-2007 11:24
is_callable also takes the php.ini "disable_functions" setting into consideration; it will return false for functions that have been disabled by your administrator.
hcblue
25-Aug-2006 05:51
True that method_exists() is faster than is_callable(). However, is_callable() will be able to correctly recognize method calls handled by __call() in PHP 5, while method_exists() will not.
jphp at dsf dot org dot uk
12-Feb-2006 08: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 11: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(
           
// Initialise loads of stuff.. including..
           
$this->tools = new myTools();
        )
       
        function
getMySource(){
           
// This just returns the source.. ok, like some HTML to go into an email for example.
            // Some arguments;
           
$args   = $this->flibble;
           
// Call our Tool that returns the source..
           
$source = $this->tools->returnSource( Array ( $this, 'someHTML' ), $args );
           
// and return it..
           
return ( $source );
        }
       
        function
someHTML($args){
           
// Leave PHP
           
?>
            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.
            <?
           
// .. and we're back.
       
}
    }
   
?>

FILE 2:

<?
   
/* OK - this is some other big library and a whole load more
       faff but this is the particular function in question... it
       just calls the function it's been asked to and uses an output
       buffer to grab the output and return it as a string rather than
       letting it go to terminal/browser.... useful for grabbing PHP pages
       for spidering, emailing etc. etc. etc..
    */
   
   
class myTools(){
       
        function
returnSource($func, $args){
           
            if (
is_callable ( $func, true, $callMe ){
               
// Start a buffer
               
ob_start();
               
// Calling the function like this DOESN'T seem to work:
                // ~~~~~~~~
                // "Command not found : myClass::someHTML"
                // $callMe($args);
                // ~~~~~~~~
                // But - what I've discovered is that this behaves fine..
               
if ( is_array($func) ){
                   
$func[0]->$func[1]($args);
                } else {
                   
$func($args);
                }
               
// Then we just carry on with our code..
               
$rtn =     ob_get_contents();
               
ob_clean();
                return (
$rtn );
            } else {
               
error_log("Doh!");
            }
        }
    }
   
?>
empyone at tiscalinet dot it
09-Nov-2005 05: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 08: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
19-Mar-2005 08: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
11-Feb-2005 12: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
24-Jun-2004 01: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
04-Apr-2004 07: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..

is_double> <is_buffer
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites