PHP Conference Fukuoka 2025

is_callable

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

is_callable 引数が、現在のスコープから関数としてコール可能な値かどうかを調べる

説明

is_callable(mixed $value, bool $syntax_only = false, string &$callable_name = null): bool

value の内容が callable かどうか、 または call_user_func() を使ってコールできるかを調べます。

パラメータ

value

チェックする値。

syntax_only

true の場合、この関数は単に value が関数またはメソッドであるかどうかだけを調べます。 invokable オブジェクト、 Closure、文字列、 コールバックとして使える有効な構造の配列でない値は拒否されます。 有効な配列の形式は、 最初のエントリがオブジェクトあるいは文字列で、 2 番目のエントリが文字列である 2 つのエントリからなるものです。

callable_name

"呼び出し名" を受け取ります。下の例では "SomeClass::someMethod" です。 これは SomeClass::someMethod() 呼び出し可能な static メソッドであるかのようにみえますが、 そうではないことに注意しましょう。

戻り値

value がコール可能な場合に true、 それ以外の場合に false を返します。

例1 文字列が関数としてコールできるかを調べる

<?php

function someFunction() {}

$functionVariable = 'someFunction';

var_dump(is_callable($functionVariable, false, $callable_name));

var_dump($callable_name);

?>

上の例の出力は以下となります。

bool(true)
string(12) "someFunction"

例2 配列が関数としてコールできるかを調べる

<?php

class SomeClass
{
public function
someMethod() {}
}

$anObject = new SomeClass();

$methodVariable = [$anObject, 'someMethod'];

var_dump(is_callable($methodVariable, true, $callable_name));

var_dump($callable_name);

?>

上の例の出力は以下となります。

bool(true)
string(21) "SomeClass::someMethod"

例3 is_callable() とコンストラクタ

コンストラクタは、 オブジェクトが作成される際にコールされるメソッドであるにも関わらず、 static メソッドではありませんし、 is_callable() はコンストラクタに対して false を返します。 特定のクラスが現在のスコープからインスタンス可能かをチェックする用途に is_callable() は使えません。

<?php

class Foo
{
public function
__construct() {}

public function
foo() {}
}

var_dump(
is_callable(['Foo', '__construct']),
is_callable(['Foo', 'foo'])
);

$foo = new Foo();
var_dump(is_callable([$foo, '__construct']));

?>

上の例の出力は以下となります。

bool(false)
bool(false)
bool(true)

注意

  • オブジェクトは、 __invoke() を実装していれば常にコール可能ですし、 そのメソッドは現在のスコープからアクセス可能です。
  • __callStatic() を実装したクラスの場合、そのクラス名はコール可能です。
  • __call() を実装しているオブジェクトが存在する場合、 この関数は、そのオブジェクトのあらゆるメソッドに対して true を返します。 たとえそのメソッドが定義されていなくてもです。
  • この関数を、クラス名を指定してコールすると、 オートローディングが行われる可能性があります。

参考

  • call_user_func() - 最初の引数で指定したコールバック関数をコールする
  • function_exists() - 指定した関数が定義されている場合に true を返す
  • method_exists() - クラスメソッドが存在するかどうかを確認する

add a note

User Contributed Notes 2 notes

up
34
izharaazmi at gmail dot com
9 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:<?phpclass 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: TRUEecho is_callable(array($t, 'testingOther'));  // Output: TRUEecho is_callable(array($t, 'working'));       // Output: TRUE, expected: FALSE?>
up
17
mohamed dot elidrissi at protonmail dot com
4 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:<?phpclass Test{    public function method1() { }    public static function method2() { }}// Pre PHP 8var_dump(is_callable(array('Test', 'method1'))); // bool(true)var_dump(is_callable(array('Test', 'method2'))); // bool(true)// Post PHP 8var_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)?>
To Top