PHP Warnings to Runtime Errors
In the transition from PHP 7.4 to PHP 8.2, notable changes have occurred with certain runtime errors that were previously treated as warnings by PHP. As of PHP 8.x, these errors have been elevated to runtime errors.
Automated tools like Rector can do a lot when it comes to lexical scanning but runtime (non-lexical and more closely tied to the logic and data aspects of code execution) its help is limited.
Defensive coding techniques serve as a robust shield against runtime errors, empowering you to anticipate and prevent such issues.
In our research, we've identified several of these techniques designed to assist you in addressing and rectifying potential errors.
Enable PHP logs
The starting point to identify such errors is to enable PHP logs. This foundational step will provide the necessary data (warnings/errors) so you can implement effective defensive coding to prevent them to happen.
E_WARNING and E_DEPRECATION Log in to the PHP error log and test your code.-
Locate your
php.inifile. The location can vary based on your operating system and PHP installation. Common paths include:- Windows:
C:\Program Files\PHP\php.ini - Linux:
/etc/php/{version}/apache2/php.ini
- Windows:
-
Open the
php.inifile in a text editor with administrative privileges. -
Search for the
error_reportingdirective. -
Update the directive to include
E_WARNINGandE_DEPRECATED:
error_reporting = E_ALL & ~E_NOTICE | E_WARNING | E_DEPRECATED