まだらもよう

Qiitaに投稿できないメモ書きなど

is_callable()

is_callable ( mixed $var ) : bool

関数としてコール可能かを返す。アクセス修飾子も考慮してくれる(protectedなどで実行できないならfalse)。

ex: LaravelのExceptionHandler

    public function report(Exception $e)
    {
        if ($this->shouldntReport($e)) {
            return;
        }

        if (is_callable($reportCallable = [$e, 'report'])) {
            return $this->container->call($reportCallable);
        }

        try {
            $logger = $this->container->make(LoggerInterface::class);
        } catch (Exception $ex) {
            throw $e;
        }

        $logger->error(
            $e->getMessage(),
            array_merge($this->context(), ['exception' => $e]
        ));
    }

似た関数にmethod_exists ( mixed $object , string $method_name ) : boolがある。
こちらはメソッドが定義されているかを返すだけで、コールできるかどうかは考慮しない。