is_callable

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

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

?>

참고

add a note add a note

User Contributed Notes 6 notes

up
31
izharaazmi at gmail dot com
8 years ago
If the target class has __call() magic function implemented, then is_callable will ALWAYS return TRUE for whatever method you call it.
is_callable does not evaluate your internal logic inside __call() implementation (and this is for good).
Therefore every method name is callable for such classes.

Hence it is WRONG to say (as someone said):
...is_callable will correctly determine the existence of methods made with __call...

Example:
<?php
class TestCallable
{
    public function
testing()
    {
          return
"I am called.";
    }

    public function
__call($name, $args)
    {
        if(
$name == 'testingOther')
        {
                return
call_user_func_array(array($this, 'testing'), $args);
        }
    }
}

$t = new TestCallable();
echo
$t->testing();      // Output: I am called.
echo $t->testingOther(); // Output: I am called.
echo $t->working();      // Output: (null)

echo is_callable(array($t, 'testing'));       // Output: TRUE
echo is_callable(array($t, 'testingOther'));  // Output: TRUE
echo is_callable(array($t, 'working'));       // Output: TRUE, expected: FALSE
?>
up
2
rahadotaboulfethatgmail.com
16 years ago
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
up
1
mohamed dot elidrissi at protonmail dot com
2 years ago
Note that -- as mentioned in the migration guide-- starting from PHP 8.0, is_callable() will not work with non-static methods if you use a class name, instead an object of the class should be provided:

<?php

class Test
{
    public function
method1() { }
    public static function
method2() { }
}

// Pre PHP 8
var_dump(is_callable(array('Test', 'method1'))); // bool(true)
var_dump(is_callable(array('Test', 'method2'))); // bool(true)

// Post PHP 8
var_dump(is_callable(array('Test', 'method1'))); // bool(false)
var_dump(is_callable(array('Test', 'method2'))); // bool(true)
var_dump(is_callable(array(new Test, 'method1'))); // bool(true)

?>
up
0
jlh
5 years ago
The story about __call() is a bit more complicated unfortunately. It will always return true ONLY if you pass an instance of a class, not if you pass the class name itself:

<?php
class MyClass {
    public function
method() { }
    public function
__call($name, $arguments) { }
}

is_callable([MyClass::class, 'method']); // true
is_callable([new MyClass(), 'method']); // true
is_callable([MyClass::class, 'other']); // false!!!
is_callable([new MyClass(), 'other'])); // true
up
0
fgm at osinet dot fr
12 years ago
Note that, for the purpose of this function, an abstract method, although necessarily non-callable since it does not have a body, is still considered to be callable:

<?php
abstract class Foo {
  abstract function
bar();
}

echo
is_callable(array('Foo', 'bar'));
// display: 1
?>
up
0
Quis strrev TA omicidio strrev TOD com
16 years ago
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
To Top