将PHP错误处理程序限制为特定的命名空间 [英] Limit PHP error handler to specific namespace(s)

查看:89
本文介绍了将PHP错误处理程序限制为特定的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中有什么办法可以为特定的命名空间设置错误处理程序吗?我正在构建一个小框架,我想通过设置自定义错误处理程序并抛出异常来尝试捕获其名称空间内的所有错误/警告/通知消息。
在此特定命名空间外触发的错误应以正常方式运行。



可以用PHP完成吗?



谢谢。

解决方案

提前道歉,不要在发布之前实际尝试过:



第五个(可选)参数,PHP传递给您的错误处理程序方法(通常被忽略)是一个数组$ errcontext,指向当前的PHP符号表。我的想法是,如果您可以从Rudie提出的异常的回溯中提取命名空间,那么也可以从$ errcontext以类似的方式提取命名空间信息。如果这是真的,那么你的错误处理程序可以检查自己的命名空间,如果当前命名空间与错误处理程序的命名空间不匹配,返回false。

此外,错误处理程序可以堆叠,这意味着至少原则上(如果我的建议我的$ errcontext实际工作),您可以为每个命名空间设置一个单独的错误处理程序。



我不是声称这种方法比Josh和Rudie提出的解决方案更为优雅,但它似乎与您尝试做的一致 - 这是对您的错误处理程序施加一种范围限制。



祝你好运!


is there any way in PHP to set error handler only for specific namespace(s)? I'm building a small framework and I'd like to be able to try-catch all error/warning/notice messages within it's namespace by setting custom error handler and throwing exceptions with it. Errors triggered outside this specific namespace should behave in a regular way.

Can it be done with PHP?

Thank you.

解决方案

Apologies in advance for not actually trying this out before posting:

The fifth (optional) parameter which PHP passes to your error handler method (and which is typically ignored) is an array $errcontext that points to the current PHP symbol table. My thought is that if you can extract the namespace from the backtrace of an Exception as proposed by Rudie then it may well also be possible to extract namespace information in a similar way from $errcontext. If that's true then your error handler can check its own namespace and "fall through" by returning false if the current namespace doesn't match the one for which the error handler is designed.

Also, error handlers can be "stacked" which means that at least in principle (if my suggestion to "mine" $errcontext actually works) you could set a separate error handler for each namespace.

I'm not claiming that this approach is any more "elegant" than the solutions proposed by Josh and Rudie, but it does seem to be in line with what you're trying to do - which is to impose a sort of scoping constraint on your error handler(s).

Good luck!

这篇关于将PHP错误处理程序限制为特定的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆