Fungsi is_callable

Fungsi is_callable menyemak sama ada nilai yang dilalui boleh dipanggil sebagai fungsi. Ia mengembalikan true jika nilai adalah:

Sintaks

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

Contoh

Semak fungsi biasa:

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

Keputusan pelaksanaan kod:

phptrue

Contoh

Semak kaedah kelas:

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

Keputusan pelaksanaan kod:

phptrue

Contoh

Semak fungsi tidak wujud:

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

Keputusan pelaksanaan kod:

phpfalse

Contoh

Semak objek dengan __invoke:

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

Keputusan pelaksanaan kod:

phptrue

Lihat juga