Functie is_callable

De functie is_callable controleert of de doorgegeven waarde als een functie kan worden aangeroepen. Het retourneert true als de waarde bestaat uit:

Syntaxis

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

Voorbeeld

Controle van een gewone functie:

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

Resultaat van de code-uitvoering:

phptrue

Voorbeeld

Controle van een klassemethode:

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

Resultaat van de code-uitvoering:

phptrue

Voorbeeld

Controle van een niet-bestaande functie:

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

Resultaat van de code-uitvoering:

phpfalse

Voorbeeld

Controle van een object met __invoke:

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

Resultaat van de code-uitvoering:

phptrue

Zie ook