近期把PHP升级到了最新稳定版本7.2.8。访问博客首页提示了两个Deprecated错误, 关于函数create_function()和__autoload(),错误信息以及对应的代码如下
错误一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // Deprecated: Function create_function() is deprecated in /var/www/html/niliu/wp-includes/pomo/translations.php on line 208 /** * Makes a function, which will return the right translation index, according to the * plural forms header * @param int $nplurals * @param string $expression */ function make_plural_form_function( $nplurals , $expression ) { $expression = str_replace ( 'n' , '$n' , $expression ); $func_body = " \ $index = (int)( $expression ); return (\ $index < $nplurals )? \ $index : $nplurals - 1;"; return create_function( '$n' , $func_body ); } |
错误二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /var/www/html/niliu/wp-includes/compat.php on line 502 // SPL can be disabled on PHP 5.2 if ( ! function_exists( 'spl_autoload_register' ) ): $_wp_spl_autoloaders = array (); /** * Autoloader compatibility callback. * * @since 4.6.0 * * @param string $classname Class to attempt autoloading. */ function __autoload( $classname ) { global $_wp_spl_autoloaders ; foreach ( $_wp_spl_autoloaders as $autoloader ) { if ( ! is_callable ( $autoloader ) ) { // Avoid the extra warning if the autoloader isn't callable. continue ; } call_user_func( $autoloader , $classname ); // If it has been autoloaded, stop processing. if ( class_exists ( $classname , false ) ) { return ; } } } endif ; |
create_function()这个问题可以换一个写法,但是__autoload是WordPress对于PHP5.2版本的一个兼容,除非移除这个方法,这样就破坏了原有的代码的完整性,于是想着先隐藏这个提示:
1 2 | error_reporting (E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED); ini_set ( 'display_errors' , 'Off' ); |
入口文件加上了上面这行代码,发现首页还是提示错误。(原因没有查到)
想到的临时解决办法如下:
在create_function前面加@, 把上面第一个代码片段第13行改为:
1 | return @create_function( '$n' , $func_body ); |
回头用匿名函数实现了,替换掉,搜索发现用create_function有一个安全漏洞,点击查看。
把__autoload改为__autoload2,把上面第二个代码片段第14行改为:
1 | function __autoload2( $classname ) { |
如果大家也遇到这个问题,可以参考这种临时解决办法,如有更好的建议,欢迎留言讨论。