php 屏蔽deprecated,php7.2.8 Deprecated错误不能隐藏处理
近期把PHP升级到了最新稳定版本7.2.8。访问博客首页提示了两个Deprecated错误, 关于函数create_function()和__autoload(),错误信息以及对应的代码如下错误一:// Deprecated: Function create_function() is deprecated in /var/www/html/niliu/wp-includes/pomo/trans
近期把PHP升级到了最新稳定版本7.2.8。访问博客首页提示了两个Deprecated错误, 关于函数create_function()和__autoload(),错误信息以及对应的代码如下
错误一:
// 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);
}
错误二:
// 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版本的一个兼容,除非移除这个方法,这样就破坏了原有的代码的完整性,于是想着先隐藏这个提示:
error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED);
ini_set('display_errors', 'Off');
入口文件加上了上面这行代码,发现首页还是提示错误。(原因没有查到)
想到的临时解决办法如下:
在create_function前面加@, 把上面第一个代码片段第13行改为:
return @create_function('$n', $func_body);
回头用匿名函数实现了,替换掉,搜索发现用create_function有一个安全漏洞,点击查看。
把__autoload改为__autoload2,把上面第二个代码片段第14行改为:
function __autoload2( $classname ) {
如果大家也遇到这个问题,可以参考这种临时解决办法,如有更好的建议,欢迎留言讨论。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)