假设我有一个父类:
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
旁注:请不要捕获 Exception
类,因为它太笼统了,稍后会使调试变得生不如死(“为什么这个方法返回一个数组??”)
关于php - 捕获和处理子类中抛出的异常的通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45935901/