PHP版本升级导致的兼容性问题
适用环境:
- php版本从5.2升级到5.5之后,原有的ecshop在运行过程中出现各种错误提示。
Ecshop如何解决Deprecated: preg_replace()报错
今天安装Ecshop后,运行出现各种问题,其中 Deprecated: preg_replace() 之类的报错最多,下面贴出解决方案:
错误原因:
preg_replace() 函数中用到的修饰符 /e 在 PHP5.5.x 中已经被弃用了。
如果你的PHP版本恰好是PHP5.5.X,那你的ECSHOP肯定就会报类似下面这样的错误:
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in......
解决办法:
1. Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in \includes\cls_template.php on line 300
原有内容:
return preg_replace("/{([^\}\{]*)}/e", "\$this->select('\\1');", $source);
修改后内容:
return preg_replace_callback("/{([^\}\{]*)}/", function($r) { return $this->select($r[1]); }, $source);
2. Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in \includes\cls_template.php on line 491
原有内容:
$out = "<?php " . '$k = ' . preg_replace("/(\'\\$[^,] )/e" , "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";";
修改后内容:
$out = "<?php " . '$k = ' . preg_replace_callback("/(\'\\$[^,] )/" , function($match){return stripslashes(trim($match[1],'\''));}, var_export($t, true)) . ";"
3. Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in \includes\cls_template.php on line 550
原有内容:
$val = preg_replace("/\[([^\[\]]*)\]/eis", "'.'.str_replace('$','\$','\\1')", $val);
修改后内容:
$val = preg_replace_callback('/\[([^\[\]]*)\]/is',function ($matches) {return '.'.str_replace('$','\$',$matches[1]);},$val);
4. Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in \includes\cls_template.php on line 1074
原有内容:
$source = preg_replace($pattern, $replacement, $source);
修改后内容:
$source = preg_replace_callback($pattern, function ($matches) { return '{include file='.strtolower($matches[1]). '}';},$source);
5. Strict Standards: Only variables should be passed by reference in ...\upload\includes\lib_main.php on line 1329
原有内容:
$ext = end(explode('.', $tmp));
修改后内容:
$extsub = explode('.', $tmp);
$tmp = end($extsub);
$tmp = basename($tmp,".$ext");
最后,将错误修改后,上传到服务器.然后进入后台,清空缓存,刷新页面即可。
php中出现Strict Standards: Only variables should be passed by reference in的解决方法
这种报错主要是因为PHP5.3以及PHP5.3以上默认只能传递具体的变量,而不能通过函数返回值传递,当然也不是所有的PHP函数都不支持这种写法,具体哪些支持哪些不支持我也不知道,但是遇到这种错误通常有两种解决办法
1.降低PHP版本到5.3以下(基本不会使用)
2.换一种写法,例如:
$list = '1x23x56';
$res = end(explode('x',$list)); #会报错,试验过end next prev reset sort这几个都会报错
$mid = explode('x',$list);
$res = end($mid); #不会报错
当然还有资料显示可以修改php.ini里面修改error_reporting,改成error_reporting=E_ALL & ~E_STRICT,意思是显示所有除了严格模式的错误,个人认为不是好办法
PHP Strict standards: Redefining already defined constructor for class问题
这个问题是PHP高低版本中,对于类的构造函数写法不一样造成的。
在PHP4中,类的构造函数是一个和类名同名的函数作为构造函数的,
而在PHP5以后,用__construct()作为构造函数,但同时也支持PHP4中用类同名的构造函数方法,
同时使用的时候,请注意 同名函数不能放在__construct()构造函数前面。
class test
{
function __construct() //此构造函数须写在前
{
//php代码
}
function test() //此函数须写在后
{
//php代码
}
}
如果把 同名函数写在前面,就会出现如下提示:
Strict standards: Redefining already defined constructor for class....
ecshop后台数据备份 Strict standards: Non-static method cls_sql_dump::get_random_name() should not be called statically in
Strict standards: Non-static method cls_sql_dump::get_random_name() should not be called statically in D:\wamp\www\yjy_shop\admin\database.php on line 64
这个意思是说非静态方法::cls_sql_dump get_random_name()
既然这样,那我们就好解决了,找到admin\includes\cls_sql_dump.php 480行
function get_random_name()
{
$str = date('Ymd');
for ($i = 0; $i < 6; $i++)
{
$str .= chr(mt_rand(97, 122));
}
return $str;
}
在前面添加上 static
static function get_random_name()
{
$str = date('Ymd');
for ($i = 0; $i < 6; $i++)
{
$str .= chr(mt_rand(97, 122));
}
return $str;
}
问题一:商城首页报错 Strict Standards: Only variables should be passed by reference in D:\wamp\ecshop\includes\cls_template.php on line 422
解决方法:
找到提示错误的文件 cls_template.php 及行号
把 $tag_sel = array_shift(explode(' ', $tag));
改成:
$tag_arr = explode(' ', $tag);
$tag_sel = array_shift($tag_arr);
并且删除 D:\wamp\www\ecshop\temp\caches下所有的文件
问题二:后台首页报错 Strict Standards: Non-static method cls_image::gd_version() should not be called statically in D:\wamp\www\ecshop\includes\lib_base.php on line 346
解决办法
找到D:\wamp\www\ecshop\includes\cls_image.php文件
搜索 function gd_version 改成 static function gd_version
问题三:后台-商店设置
Strict Standards: mktime(): You should be using the time() function instead in D:\wamp\www\ecshop\admin\sms_url.php on line 31
Strict Standards: mktime(): You should be using the time() function instead in D:\wamp\www\ecshop\admin\shop_config.php on line 32
解决办法
根据错误提示 把 mktime() 改成 time()
问题四:后台-起始页
Strict Standards: Redefining already defined c**tructor for class alipay in D:\www\es\includes\modules\payment\alipay.php on line 85
解决办法
1)、错误原因:
PHP 类,有两种构造函数,一种是跟类同名的函数,一种是 __contruct()。从PHP5.4开始,对这两个函数出现的顺序做了最严格的定义,必须是 __c**truct() 在前,同名函数在后
2)、
解决方法:
调换一下两个函数的前后位置即可。
以 includes/modules/payment/alipay.php 为例:
将下面这两个函数的位置互换一下就OK了,__contruct()在前,alipay()在后
function alipay() {
}
function __contruct()
{
$this->alipay();
}
3)、ECSHOP的很多类文件 都存在这个问题,都需要修改掉。
问题五:后台-数据备份
Strict standards: Redefining already defined constructor for class cls_sql_dump in D:\wamp\www\ecshop\admin\includes\cls_sql_dump.php on line 90
Strict standards: Non-static method cls_sql_dump::get_random_name() should not be called statically in D:\wamp\www\ecshop\admin\database.php on line 64
解决办法
根据错误提示 把 cls_sql_dump的 function __construct()改到 function cls_sql_dump()的前面
把 cls_sql_dump的 function get_random_name()改成 static function get_random_name()
问题六:
Deprecated: Assigning the return value of new by reference is deprecated in \admin\sitemap.php on line 46
$sm =& new google_sitemap();
解决办法
在5.3版本之后已经不允许在程序中使用”=&”符号。如果你的网站出现了Deprecated: Assigning the return value of new by reference is deprecated in 错误,别着急,先定位到出错的文件,查找下是不是在程序中使用了”=&”,例如刚才定位到网站程序中发现了下图的程序,发现使用了”=&”符号,去掉‘&’符号之后程序运行正常
问题七:
Declaration of phpbb::set_cookie() should be compatible with integrate::set_cookie...
解决办法:把function set_cookie ($username="") 改为function set_cookie ($username="", $remember = NULL)即可
问题八:
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in..
解决办法:
我遇见了有两处,都在cls_template.php文件中:
1、return preg_replace("/{([^\}\{\n]*)}/e", "\$this->sel ect('\\1');", $source);
替换为
return preg_replace_callback("/{([^\}\{\n]*)}/", function($r) { return $this->sel ect($r[1]); }, $source);
问题解决。
2、$val = preg_replace("/\[([^\[\]]*)\]/eis", "'.'.str_replace('$','\$','\\1')", $val);
替换为
$val= preg_replace_callback("/\[([^\[\]]*)\]/eis",function($r){return str_replace('$','\$',$r[1]);}, $val);
问题解决
ECSHOP生成站点地图提示”Deprecated: Assigning the return value of new by reference is deprecated in…”。
定位到报错行
$sm =& new google_sitemap();
$smi =& new google_sitemap_item($domain, $today, $_POST['homepage_changefreq'], $_POST['homepage_p...
PHP5.3+废除了”=&”符号,对象复制用”=”即可,详细如下:
PHP5对象复制是采用引用的方式。
如果不采用引用方式,则需要在复制对象时加关键字 clone。
如果在复制的过程中,同时要变更某些属性,则增加函数_clone()。
解决办法:搜索所有PHP文件,将”=&”替换为”=”。