Search the Community
Showing results for tags 'php errors'.
-
If you want, you can filter them out and focus more on your essentials. But first things first. There is a whole list of different PHP errors, warnings and hints (notices). The one we are interested in is E_DEPRECATED. These messages indicate that a used PHP function somewhere in the code is no longer supported in a future, i.e. higher PHP version, not the actual used one. Example: The days (February 2022) I have changed my developer machine to use the latest PHP version 8.1.2. The last PHP 7.4+ is still supported for 9 months from today on, PHP 8.0 still for 1 year + 9months, PHP 8.1 for 2 years+ 9 months, and all other PHP 8+ versions each plus one more year. So the point is clear now, right? It will be year(s) before PHP 9 is on the schedule. ? Currently in v8.1.2, depending on what code I run, I get notices that certain PHP functions will no longer be available starting with PHP 9. (PHP 9! see above). So currently these functions are safe and supported functions, as in past versions as well as in all further PHP 8+ versions(!). No matter how many additional PHP 8+ versions there will be. ProcessWire by default allows us to suppress ALL output, or show ALL output, by the boolean configuration variable "$config->debug". This is good and right, because on a production site you should always suppress all those outputs. On a developer machine it usually shouldn't bother you if there are also E_DEPRECATED hints included. But if it does, you can filter them out as follows, for example. In your site/config.php you can define a own error handler function and load / bind it on your developer machine only. As a proof of concept or simple example, I added the following function to my site/config.php /** OWN ERROR HANDLER FOR DEPRECATION NOTES **********************************************/ function myLocalDevDeprecationErrorHandler($errno, $errstr, $errfile, $errline) { if(!isset($GLOBALS['DEPRECATION_WARNINGS_BAG'])) { $GLOBALS['DEPRECATION_WARNINGS_BAG'] = []; } if(!is_array($GLOBALS['DEPRECATION_WARNINGS_BAG'])) { $GLOBALS['DEPRECATION_WARNINGS_BAG'] = []; } $GLOBALS['DEPRECATION_WARNINGS_BAG'][] = [$errfile, $errline, $errstr]; return true; // true | false = suppress further processing } /** OWN ERROR HANDLER FOR DEPRECATION NOTES **********************************************/ With the following call, also located in my developer machines site/config.php file, I bind it to all raising E_DEPRECATED warnings ONLY! So we are sure and free from all other types of errors, warnings, hints, etc. // preceed the callback function name with the PW namespace, // and define the error types that should passed to the function set_error_handler('ProcessWire\myLocalDevDeprecationErrorHandler', E_DEPRECATED); (read more on php.net) Now you will not see any of the deprecated notices any more, cluttering your screen. Neither in the front end nor in the backend. But they are all collected in our bag. ? A simple loop in the site/templates/admin.php, before the controller.php is called, can display any of it, maybe to the superusers only, for example: <?php namespace ProcessWire; // IF WE HAVE CACHED PHP DEPRECATION WARNINGS, SHOW THEM IN THE ADMIN, FOR SUPERUSERS ONLY if(isset($GLOBALS['DEPRECATION_WARNINGS_BAG']) && is_array($GLOBALS['DEPRECATION_WARNINGS_BAG']) && 0 < count($GLOBALS['DEPRECATION_WARNINGS_BAG']) && $user->isSuperuser()) { foreach($GLOBALS['DEPRECATION_WARNINGS_BAG'] as $deprecatedMessageItem) { $deprecatedMessageItemMSG = "PHP DEPRECATED (v". PHP_VERSION .") = \n "; $deprecatedMessageItemMSG .= "{$deprecatedMessageItem[0]} : [{$deprecatedMessageItem[1]}]\n "; $deprecatedMessageItemMSG .= "{$deprecatedMessageItem[2]}"; $this->message($deprecatedMessageItemMSG); } } require($config->paths->adminTemplates . 'controller.php'); If you want to show them on the front end to, it belongs to how you have organized your output strategy. I use a delayed output with _init.php and _main.php, where I have a special function for adding all kind of debug messages, that gets displayed on the end of the site, after the footer. Simply adding the whole collected array is enough for my information: if(isset($GLOBALS['DEPRECATION_WARNINGS_BAG']) && is_array($GLOBALS['DEPRECATION_WARNINGS_BAG']) && 0 < count($GLOBALS['DEPRECATION_WARNINGS_BAG'])) { mvdr(['DEPRECATED ('. PHP_VERSION .')' => $GLOBALS['DEPRECATION_WARNINGS_BAG']]); // add it to the debug output region var } The outputs in admin then may look like this Or the very basic info for me on front end
- 16 replies
-
- 6