具体而言,我提供了一些代码,我知道我想要的代码是不可能的。我正在寻找另一种方法来获得相同的结果。
<?php
error_reporting(E_ALL | E_STRICT);
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
// When $errno is a notice, we would like a way for the execution to continue here. Since it is only a notice, eventually it could go back to where the notice was triggered.
}
set_error_handler("exception_error_handler");
Class TopLevelManagement {
private $genericInfo = "Very important to add in notices";
function highleveljob() {
try{
// In practice, the following statements could occur below in the execution tree, after a few other function calls.
$specific = new SpecificAndEncapsulated();
$specific->specificjob();
}
catch (ErrorException $e) {
$message = $e->getMessage() . $this->genericInfo;
mail($admin_email, "Consider the generic info and recover from this situation", $message);
// Here we would like the execution to continue where the exception was thrown. In the real world, this would be like "thank you error handler for SpecificAndEncapsulated::specificjob, we have taken care of the issue with the help of our larger perspective.".
}
}
}
Class SpecificAndEncapsulated {
function specificjob() {
// Some processing
if($unexpected == true) trigger_error('Here is how little I know, as I should', E_USER_NOTICE);
// Continue as expected after a notice.
}
}
?>
当然,一种解决方案是$genericInfo
作为参数或全局变量传递给SpecificAndEncapsulated::justdomyjob
error_handler 并让 error_handler 处理问题而不会引发任何异常。然而,这种解决方案并不自然。还有其他方法可以系统地将变量传递$genericInfo
给SpecificAndEncapsulated
,但问题是一样的。没有必要系统地传递 $genericInfo 值,因为它不是应该关注的事情,SpecificAndEncapsulated
即使发生异常也不应该,更不用说在每次调用时都系统地传递。在上级管理通知后,向异常发出者回复说“谢谢,现在继续”的通信是很自然的。是否支持这种类型的 E_NOTICE 或 E_USER_NOTICE 管理?