Funktion is_callable

Die Funktion is_callable prüft, ob der übergebene Wert als Funktion aufgerufen werden kann. Sie gibt true zurück, wenn der Wert Folgendes ist:

Syntax

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

Beispiel

Überprüfung einer normalen Funktion:

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

Ergebnis der Codeausführung:

phptrue

Beispiel

Überprüfung einer Klassenmethode:

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

Ergebnis der Codeausführung:

phptrue

Beispiel

Überprüfung einer nicht existierenden Funktion:

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

Ergebnis der Codeausführung:

phpfalse

Beispiel

Überprüfung eines Objekts mit __invoke:

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

Ergebnis der Codeausführung:

phptrue

Siehe auch

  • die Funktion function_exists,
    die die Existenz einer Funktion prüft
  • die Funktion method_exists,
    die die Existenz einer Klassenmethode prüft