33 of 410 menu

The is_callable Function

The is_callable function checks if the passed value can be called as a function. It returns true if the value is:

  • a function name as a string
  • an array with an object and a method name
  • an object with the __invoke method
  • an anonymous function

Syntax

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

Example

Checking a regular function:

<?php function test() {} $res = is_callable('test'); var_dump($res); ?>

Code execution result:

true

Example

Checking a class method:

<?php class MyClass { public function method() {} } $obj = new MyClass(); $res = is_callable([$obj, 'method']); var_dump($res); ?>

Code execution result:

true

Example

Checking a non-existent function:

<?php $res = is_callable('non_existent_function'); var_dump($res); ?>

Code execution result:

false

Example

Checking an object with __invoke:

<?php class Invokable { public function __invoke() {} } $obj = new Invokable(); $res = is_callable($obj); var_dump($res); ?>

Code execution result:

true

See Also

  • the function_exists function,
    which checks for the existence of a function
  • the method_exists function,
    which checks for the existence of a class method
byenru