14

How can I make Laravel 4 or 5 ignore PHP notices (like undefined variable notices) and not break the whole app only because of a simple 'undefined index or variable' PHP notice?

I could to that on Laravel 3 setting an 'ignore' array in config/error.php. But I cant find how to do that in Laravel 4 or 5.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Phius
  • 725
  • 1
  • 8
  • 15

2 Answers2

14

This behavior is due to setting error reporting to -1. This is Laravel's default behaviour - see line 14 in vendor/laravel/framework/src/illuminate/Foundation/start.php if you're using Laravel 4, or line 29 in vendor/laravel/framework/src/illuminate/Foundation/Bootstrap/HandleExceptions.php if you're using Laravel 5:

error_reporting(-1); // Reports everything

Laravel's error handler respects your error_reporting level, and will ignore any errors that you tell PHP not to report. It's worth mentioning that changing the error reporting level isn't a good idea. But to override the previous instruction you can add your error reporting preferences in the app/start/global.php (in Laravel 4) or app/bootstrap/app.php (in Laravel 5)

error_reporting(E_ALL ^ E_NOTICE); // Ignores notices and reports all other kinds

Again this isn't a solution. It's merely what you are asking for. All and any errors, warning, notices etc. can and should be fixed.

You can see all the constants for error reporting here: http://www.php.net/manual/en/errorfunc.constants.php

You can get more information on how to use error_reporting here: http://php.net/manual/en/function.error-reporting.php

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
must
  • 226
  • 2
  • 10
  • I'm using this to turn off strict mode, which I think is a non-evil usage. Strict mode [does very little for you](http://stackoverflow.com/a/25826279/1709587) in the first place and is being [removed from the language](https://wiki.php.net/rfc/reclassify_e_strict) in PHP 7, together with completely removing two of the warnings that fell under that level - importantly including the totally irrational ban on abstract static methods in strict mode. It's a questionable decision on Laravel's part to enable E_STRICT by default, in my opinion. – Mark Amery Jun 15 '15 at 16:06
  • adding `error_reporting(E_ALL ^ E_NOTICE); ` in `app/bootstrap/app.php` doesn't seems to work with laravel 5.1 – Moppo Sep 15 '15 at 14:02
13

In Laravel 5.1 you can add error_reporting(0) or whatever you want into \app\Providers\AppServiceProvider.php boot() method

Alright
  • 131
  • 1
  • 3
  • omg you have saved me so many hours of work upgrading an old laravel 4.2 app – bhu Boue vidya Jun 24 '16 at 03:08
  • 1
    @IvankaTodorova Sometimes one has to be pragmatic. When a port of an old 4.2 laravel app that was developed on php 5.4 had to be upgraded to run on php 7 and laravel 5.2, and there's only 1 day to do it, then yeh, you do what ya gotta do. – bhu Boue vidya Apr 26 '17 at 08:42
  • @bhuBouevidya But it is no longer the same app. Hiding the errors means that the app misbehaves somehow and the results will be unexpected and probably bad. I wouldn't call that *pragmatic*. – Ivanka Todorova Apr 26 '17 at 14:33
  • 1
    @IvankaTodorova I didn't turn off all errors by using `error_reporting(0)` - I just turned off `E_NOTICE` as the old code assumed it could access vars or array offsets and if they didn't exist it would just return something falsey. Worked like a charm. App runs fine. So the solution was very pragmatic. The client wasn't willing to pay me to spend the next 2-4 weeks digging out every line of code that made that assumption, and hoping I'd found every single one (and we're talking > 100,000 lines). But thanks for your judgmental comments anyway. – bhu Boue vidya Apr 29 '17 at 10:09
  • This was literally the only thing that worked for me. – Theodore R. Smith Feb 25 '20 at 16:37