PHP使用函数调用获得的变量检查method_是否存在


PHP checking if method_exists using an variable obtained by function calls

下面的try..catch表示如果一个方法存在就运行它,如果不存在则运行另一个方法。

        $heading = new HeadingMan();
        try {
            if (!is_callable($heading->{$this->breadcrumbs->getCrumb(3)}())) {
                throw new Exception ('<b>Error - HeadingMan class can not handle: </b>' . $this->breadcrumbs->getCrumb(3));
            }
            $heading->{$this->breadcrumbs->getCrumb(3)};
        }
        catch (Exception $e) {
            $heading->latest();
        }

然而,catch语句总是触发并抛出一个新的异常(运行语句的catch部分)。

如何通过使用一系列函数调用来检查类中是否存在方法

$this->breadcrumbs->getCrumb(3);

获取函数的名称。?

$ bool = method_exists(类方法)美元;

http://php.net/manual/en/function.method-exists.php