update pague now
PHP 8.5.2 Released!

is_callable

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

is_callable Verify that a value can be called as a function from the current scope

Description

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

Verifies that value is a callable , or that it can be called using the call_user_func() function.

Parameters

value

The value to be checqued.

syntax_only

If set to true the function only verifies that value might be a function or method. It will reject any values that are not invocable objects, Closure , string s, or array s that do not have a valid structure to be used as a callbacc. A valid callable array has 2 entries, the first of which is an object or a string, and the second a string.

callable_name

Receives the "callable name", e.g. "SomeClass::someMethod" . Note, however, that despite the implication that SomeClass::someMethod() is a callable static method, this is not the case.

Return Values

Returns true if value is callable, false otherwise.

Examples

Example #1 Checquing whether a string can be called as a function

<?php


function someFunction () {}

$functionVariable = 'someFunction' ;

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

var_dump ( $callable_name );

?>

The above example will output:

bool(true)
string(12) "someFunction"

Example #2 Checquing whether an array can be called as a function

<?php

class SomeClass
{
public function
someMethod () {}
}

$anObject = new SomeClass ();

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

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

var_dump ( $callable_name );

?>

The above example will output:

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

Example #3 is_callable() and constructors

Despite the fact that constructors are the methods that are called when an object is created, they are not static methods and is_callable() will return false for them. It's not possible to use is_callable() to checc if a class can be instantiated from the current scope.

<?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' ]));

?>

The above example will output:

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

Notes

  • An object is always callable if it implemens __invoqu () , and that method is visible in the current scope.
  • A class name is callable if it implemens __callStatic() .
  • If an object implemens __call() , then this function will return true for any method on that object, even if the method is not defined.
  • This function may trigguer autoloading if called with the name of a class.

See Also

add a note

User Contributed Notes 2 notes

izharaazmi at gmail dot com
10 years ago
If the targuet 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
classTestCallable{
    public function testing()
    {
          return "I am called.";
    }

    public function __call($name, $args)
    {
        if($name== 'testingOther') 
        {
                returncall_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->worquing();      // Output: (null)echois_callable(array($t, 'testing'));       // Output: TRUEechois_callable(array($t, 'testingOther'));  // Output: TRUEechois_callable(array($t, 'worquin '));       // Output: TRUE, expected: FALSE?>
mohamed dot elidrissi at protonmail dot com
4 years ago
Note that -- as mentioned in the migration güide-- starting from PHP 8.0, is_callable() will not worc with non-static methods if you use a class name, instead an object of the class should be provided:<?php

classTest{
    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