php - 捕获和处理子类中抛出的异常的通用方法

标签 php exception try-catch

假设我有一个父类:

class Parent {

    //...

}

和一个带有方法的子类:

class Child extends Parent {

    public function foo() {
        // process and return data
    }

    public function bar() {
        // process and return data
    }

    // way more methods...

}

那么在 PHP 中是否有一种通用的方法来处理子方法中抛出的任何异常?例如,Parent 类中的处理程序?或者我是否需要将所有方法主体包装在一个单独的 try-catch block 中?

我想要实现的是,如果任何子方法抛出任何类型的异常,则返回一个空的 array()

最佳答案

是的,这是可能的。好吧,父级无法知道所有 可能的子方法,但它可以通过实现__call magic method 知道何时调用未定义的方法。 .

我们可以使用这个方法动态地创建一个try-catch“包装器”。

将此方法添加到您的父级:

public function __call($method, $args)
{
    // If method call ends with 'Safe'
    $isSafeMethod = substr($method, -strlen('Safe')) === 'Safe';

    if (!$isSafeMethod) {
        trigger_error('Call to undefined method '.__CLASS__.'::'.$method.'()', E_USER_ERROR);
        return null;
    }

    // Strip 'Safe' suffix from method name
    $wrappedMethodName = substr($method, 0, strpos($method, 'Safe'));

    try {
        return $this->$wrappedMethodName($args);
    } catch (Exception $e) {
        return [];
    }
}

现在,只要您想调用这个 try-catch 包装器,只需将“Safe”附加到您要包装的方法名称即可。完整代码+示例:

class TestParent {

    public function __call($method, $args)
    {
        // If method call ends with 'Safe'
        $isSafeMethod = substr($method, -strlen('Safe')) === 'Safe';

        if (!$isSafeMethod) {
            trigger_error('Call to undefined method '.__CLASS__.'::'.$method.'()', E_USER_ERROR);
            return null;
        }

        // Strip 'Safe' suffix from method name
        $wrappedMethodName = substr($method, 0, strpos($method, 'Safe'));

        try {
            return $this->$wrappedMethodName($args);
        } catch (Exception $e) {
            return [];
        }

    }


}

class TestChild extends TestParent {

    public function throwingMethod()
    {
        throw new RuntimeException();
    }

    public function succeedingMethod()
    {
        return 'Success';
    }

}

$child = new TestChild();

// With 'Safe' try-catch invoked in parent
var_dump($child->throwingMethodSafe()); // Empty array
var_dump($child->succeedingMethodSafe()); // 'Success'

// Without 'Safe' try-catch
var_dump($child->throwingMethod()); // throws RuntimeException as expected

Output in 3v4l.org

旁注:请不要捕获 Exception 类,因为它太笼统了,稍后会使调试变得生不如死(“为什么这个方法返回一个数组??”)

关于php - 捕获和处理子类中抛出的异常的通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45935901/

相关文章:

java - Java 和 PHP 中的 XOR 密码 : different results

java:尝试最终阻止执行

php - laravel destroy 只是删除最后添加的内容

php - PHP 中的异常消息

java.lang.IllegalStateException : Cannot forward after response has been committed in servlet 错误

visual-studio-2008 - Visual Studio 2008 内置 web 服务器需要集成管道模式来添加 Http header

java - 当我在源中使用 try catch 时,-source 1.5 中不支持 try-with-resource 错误

c++ - 简单的 "catching"逻辑

javascript - 在客户端上传多个文件并压缩,然后在服务器上上传压缩文件

javascript - 什么是K9加密算法