Suppress PHP notifications; only certain circumstances / methods

tl; dr - Is there an effective way to control the level of error reporting in PHP when working in a very strict environment, given that some processes will be simplified at a less strict level?

Good; Firstly, I do not believe that “suppressing errors” is a solution. I (sure I) never used the @ error suppression operator and am not going to do this. I take advantage of set_error_handler() and ErrorException (or some origin) and develop into error_reporting(-1) (future E_ALL | E_STRICT )

Now I don’t want to change these habits, because I think that they are a great practice (also; if someone has suggestions for further improvement of the settings / practices of my development environment / production environment, I’m all in my ears)

However, when it comes to generation, it can become a little tedious. The correct data (array indices, variables, etc.) are not always available, because the controller for some reason cannot pass certain data to the view. As long as this data is not critical for generating the view, the view should still be displayed.

I rather like this syntax as it is not verbose, but (I think) very clear:

 // e() is a shortcut function; given the passed value evaluates to a boolean true // it will echo() and return true, otherwise it simply returns false <p><?php e($data['field']) or e('No data found'); ?></p> 

Of course, if $data['field'] does not call offsetGet() with null returned in the absence of an index, we have a problem. Notification to meet exception, exception to meet script error.

I experimented with various implementations, including creating a data tree using a class like a node to manage lists / rows of data passed to the view. __get() will actually create nodes (upon assignment or access) that do not exist (to simplify the assignment of node data and to prevent notifications. __isset() checked for authenticity and, nonetheless, returns false accordingly) He also implemented ArrayAccess for access node data, and would just return null on the missing index.

I decided to abandon this implementation due to the overhead of PHP magic (although I learned a lot about refactoring / optimization and profiling)

Instead, I switched to my own arrays, but now the code base for my views is dotted with isset() , and frankly, it is just annoying (almost more than the performance loss of the above implementation)

Now I thought that the simplest solution would be to move error_reporting() up and down depending on where we are in the script:

 // View::render() public function render($data){ error_reporting(E_ALL & ~E_NOTICE); // view generation logic error_reporting(-1); } 

But this does not seem to be the cleanest (and not the safest) fix; especially when helper functions are called in the view. I chose a kind of HMVC approach, and error_reporting(-1) can be error_reporting(-1) from the view, so I need to find all the render() exit points and protect them with error_reporting(-1) .

Do I have other options?

+4
source share
2 answers

"undefined variables" notifications are very valuable, even in the form of templates, as they help to identify typos; but this requires either defining each variable in the controllers, or checking whether they are set in the views.

As you noticed, two obvious solutions have some overhead or disadvantages. Even disabling error messages has some overhead, since errors are still generated (error message is formatted, internal and user error handlers are called, etc., they are simply hidden). And this hides errors from helper methods that you can call from views; this does not help debug.

I would recommend you go with a template engine. Some generate PHP code as fast as handwritten code. They will handle this for you and will do more (e.g. escaping, your views should also be dotted with calls to htmlspecialchars ();)).

+3
source

Keep reporting E_NOTICE, it's worth it. However, I agree that the Undefined Index not the same error gauge as the undefined variable, and isset($options['boolean_flag']) && $options['boolean_flag'] is a bit ugly. The project I'm working on has thousands of these notifications, so in order to continue to view E_NOTICE level errors without flooding on the Undefined Index , I actually recompiled the language to ignore this type of notification. (I use HHVM, not PHP, but this is the same).

Yes, this is an extreme solution, but it is an option in a difficult place. Obviously, you will want to use the official assembly in production.

Note. I wrote down the steps to recompile and publish this if someone wants to try, but it goes a little beyond the original question.

+1
source

All Articles