PHP notice suppression; only certain circumstances/methods

ε祈祈猫儿з 提交于 2019-12-05 13:20:00

"undefined variable" notices are very valuable, even in view templates, as they help to spot typos; but this requires to either define every variable in the controllers, or to check if they are set in views.

As you noticed, the two obvious solutions have some overhead or drawbacks. Even disabling error reporting has some overhead since errors are still generated (the error message is formatted, internal and user error handlers are called, etc; they are just hidden). And this hides errors from helper methods you may call from the views; this doesn't helps debugging.

I would recommand you to go with a template engine. Some generate PHP code as fast as hand-written code. They will handle this for you, and will do more (like escaping, your views should also be littered with htmlspecialchars() calls ;) ).

Keep reporting E_NOTICE, it's worth it. That said, I agree that Undefined Index is not the same caliber of error as an undefined variable, and isset($options['boolean_flag']) && $options['boolean_flag'] is a bit ugly. A project I work on has thousands of those notices, so in order to keep seeing E_NOTICE-level errors without getting flooded by Undefined Index, I actually recompiled the language to ignore that particular type of notice. (I'm using HHVM rather than PHP, but it's the same difference).

Yeah, that's an extreme solution, but it is an option in a tight spot. Obviously you'll want to use an official build on production.

Note: I wrote down the steps to recompile and could post that if anyone would like to try, but it's a little outside the scope of the original question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!