skip to Main Content

I currently have all errors enabled for my site:

error_reporting(E_ALL);

However, in PHP 8.1, some functions are now deprecated:

PHP Notice: Function date_sunset() is deprecated in index.php on line 14

Due to current requirements, I am unable to update the line to the non-deprecated alternative.

Is there a way to disable error reporting of deprecations just for this single line?

3

Answers


  1. Chosen as BEST ANSWER

    As a workaround, you can wrap your deprecated line like this:

    error_reporting(E_ALL & ~E_DEPRECATED);
    // call_deprecated_function_here()
    error_reporting(E_ALL);
    

    Or if you wish to simply toggle the deprecated flag, use this:

    error_reporting(error_reporting() ^ E_DEPRECATED); // toggle E_DEPRECATED (off)
    // call_deprecated_function_here()
    error_reporting(error_reporting() ^ E_DEPRECATED); // toggle E_DEPRECATED (back on)
    

  2. The proposed workaround is only good until the function is removed, then it will only be fatal at some point whenever the function is removed, and a forced change will be imminent anyway.

    Why not try to use what PHP’s own API suggests?:

    Relying on this function is highly discouraged. Use date_sun_info() instead.

    See date_sun_info() … there are less parameters though, depending upon what you may have used previously with date_sunset().

    Login or Signup to reply.
  3. You can add @ infront of your deprecated function. With the @ you suppress the PHP error messages that would otherwise have been thrown at this point.

    @functionName(...)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search