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?