Home > Software design >  PHP Disable Deprecation Warnings for a Single Line
PHP Disable Deprecation Warnings for a Single Line

Time:12-09

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?

CodePudding user response:

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

error_reporting(E_ALL & ~E_DEPRECATED);
// call deprecated function
error_reporting(E_ALL);

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

error_reporting(error_reporting() ^ E_DEPRECATED);
// call deprecated function
error_reporting(error_reporting() ^ E_DEPRECATED);

CodePudding user response:

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

CodePudding user response:

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

  • Related