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:

Syntax

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

Example

Checking a regular function:

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

Code execution result:

phptrue

Example

Checking a class method:

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

Code execution result:

phptrue

Example

Checking a non-existent function:

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

Code execution result:

phpfalse

Example

Checking an object with __invoke:

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

Code execution result:

phptrue

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