Magento 1.4 and PHP Deprecated warnings
Wednesday, 24 November 2010While Magento 1.4.1.1 is fully compatible with PHP 5.3, using the PEAR command-line (as in the shell-script called pear) still throws multiple PHP Deprecated warnings. These warnings are mostly harmless but still clutter the command-line so much that it becomes unusable. But a simple trick might actually fix this.
Understanding PHP warnings
Within PHP, multiple types of messages can be generated: With a PHP Fatal Error, the execution of PHP stops. With PHP Warnings, a programmer is warned that the PHP-code still works but might cause severe problems in the future. With PHP Notices, the programmer is just notified of sloppy programming. There is also the type PHP Deprecated, which tells programmers that the application contains PHP-structures that are deprecated and that should be migrated to new code instead.
While a PHP Fatal Error is ... well, fatal - meaning that the PHP application will crash when a PHP Fatal Error is encountered, a PHP Deprecated message is not that bad at all. Actually it will not do any harm to disable those messages all together.
Switching back to PHP 5.2
Instead of focussing on how to solve these errors with PHP 5.3, a simple solution might be to switch-back to PHP 5.2. While this is not recommended for your Magento site itself (because PHP 5.3 simply just gives too many benefits), using the PEAR command-line with only PHP 5.2 is just fine. If your hosting provider offers the binaries of both PHP 5.2 as PHP 5.3, you might be able to use PHP 5.3 for the Magento application but use PHP 5.3 for the PEAR command-line.
To use a different PHP binary just for the PEAR Downloader, open up the file pear within the Magento folder and modify the following line:
MAGE_PEAR_PHP_BIN="/usr/bin/php5.2"
The example shows an example of a binary /usr/bin/php5.2. Ofcourse it depends on your hosting environment which binary-path should be entered.
Modifying error_reporting
Another way to deal with these PHP Deprecated errors is to modify the PHP setting error_reporting as it is used within the Magento code. Unfortunately this means you have to modify the PHP-code at various levels, which is less recommended than the solution above.
Fix it yourself
One final method: You could fix the actual PHP warnings themselves. For this, you need to modify the Magento PHP-code.Open up the files downloader/pearlib/php/pearcmd.php and downloader/pearlib/php/pearmage.php and locate the following section:
function error_handler($errno, $errmsg, $file, $line, $vars) {
if ((defined('E_STRICT') && $errno & E_STRICT) || (defined('E_DEPRECATED') &&
$errno & E_DEPRECATED) || !error_reporting()) {
if (defined('E_STRICT') && $errno & E_STRICT) {
return; // E_STRICT
}
if ($GLOBALS['config']->get('verbose') < 4) {
return false; // @silenced error, show all if debug is high enough
}
}
Replace this with the following:
function error_handler($errno, $errmsg, $file, $line, $vars) {
if ((defined('E_STRICT') && $errno & E_STRICT) || !error_reporting()) {
if (defined('E_STRICT') && $errno & E_STRICT) {
return; // E_STRICT
}
if (defined('E_DEPRECATED') && $errno & E_DEPRECATED) {
return; // E_DEPRECATED
}
if ($GLOBALS['config']->get('verbose') < 4) {
return false; // @silenced error, show all if debug is high enough
}
}
This simply removes the whole logging of deprecated messages, which is not a real solid solution but a fine workaround that is even incorporated in the newest Magento versions. Thanks to Mark van der Sanden for pointing this harmless core-hack out.
