3

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?

Westy92
  • 19,087
  • 4
  • 72
  • 54
  • 1
    Does this answer your question? [Turn off deprecated errors in PHP 5.3](https://stackoverflow.com/questions/2803772/turn-off-deprecated-errors-in-php-5-3) – Yogendra Jun 27 '23 at 05:45
  • @Yogendra No, my question asks specifically about turning off deprecated errors only for a single line of code. – Westy92 Jul 02 '23 at 16:33

3 Answers3

2

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)
Westy92
  • 19,087
  • 4
  • 72
  • 54
0

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().

Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • Unfortunately I cannot use the newer version because I need the additional flexibility with the additional parameters. – Westy92 Dec 11 '22 at 03:04
-1

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(...)

Max Pattern
  • 1,430
  • 7
  • 18