ThinkPHP3.1迁移到PHP7

一、在PHP7中,preg_replace不能用/e修饰符,所以用preg_replace_callback代替preg_replace

需要修改的文件包括

ThinkPHP\Lib\Template\ThinkTemplate.class.php
ThinkPHP\Lib\Core\Dispatcher.class.php
ThinkPHP\Lib\Core\Db.class.php
ThinkPHP\Lib\Behavior\CheckRouteBehavior.class.php
ThinkPHP\Extend\Mode\Lite\Dispatcher.class.php

ThinkPHP\Lib\Behavior\ReadHtmlCacheBehavior.class.php
ThinkPHP\Common\common.php


1)ThinkPHP\Lib\Template\ThinkTemplate.class.php
这个文件大约需要修改8处地方
NO1. 大约在137行,将

[php]  view plain  copy
  1. $tmplContent =  preg_replace('/<!--###literal(\d+)###-->/eis',"\$this->restoreLiteral('\\1')",$tmplContent);  
替换为
[php]  view plain  copy
  1. $tmplContent = preg_replace_callback('/<!--###literal(\d+)###-->/is'function($r) {  
  2.     return $this->restoreLiteral($r[1]);  
  3. }, $tmplContent);  

NO2. 大约在168行,将
[php]  view plain  copy
  1. $content = preg_replace('/'.$begin.'literal'.$end.'(.*?)'.$begin.'\/literal'.$end.'/eis',"\$this->parseLiteral('\\1')",$content);  
替换为
[php]  view plain  copy
  1. $content = preg_replace_callback('/' . $begin . 'literal' . $end . '(.*?)' . $begin . '\/literal' . $end . '/is'function($r) {  
  2.     return $this->parseLiteral($r[1]);  
  3. }, $content);  

NO3. 大约在197行,将
[php]  view plain  copy
  1. $content = preg_replace('/('.$this->config['tmpl_begin'].')([^\d\s'.$this->config['tmpl_begin'].$this->config['tmpl_end'].'].+?)('.$this->config['tmpl_end'].')/eis',"\$this->parseTag('\\2')",$content);  
替换为
[php]  view plain  copy
  1. $content = preg_replace_callback('/(' . $this->config['tmpl_begin'] . ')([^\d\s' . $this->config['tmpl_begin'] . $this->config['tmpl_end'] . '].+?)(' . $this->config['tmpl_end'] . ')/is'function($r) {  
  2.     return $this->parseTag($r[2]);  
  3. }, $content);  

NO4. 大约在266行,将
[php]  view plain  copy
  1. preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"\$this->parseBlock('\\1','\\2')",$content);  
替换为
[php]  view plain  copy
  1. preg_replace_callback('/' . $begin . 'block\sname=(.+?)\s*?' . $end . '(.*?)' . $begin . '\/block' . $end . '/is'function ($r) {  
  2.     $this->parseBlock($r[1], $r[2]);  
  3. }, $content);  

NO5. 大约在271行,将
[php]  view plain  copy
  1. $content = preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"\$this->replaceBlock('\\1','\\2')",$content);  
替换为
[php]  view plain  copy
  1. $content = preg_replace_callback('/' . $begin . 'block\sname=(.+?)\s*?' . $end . '(.*?)' . $begin . '\/block' . $end . '/is'function ($r) {  
  2.     return $this->replaceBlock($r[1], $r[2]);  
  3. }, $content);  

NO6. 大约在273行,将
[php]  view plain  copy
  1. $content = preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"stripslashes('\\2')",$content);  
替换为
[php]  view plain  copy
  1. $content = preg_replace_callback('/' . $begin . 'block\sname=(.+?)\s*?' . $end . '(.*?)' . $begin . '\/block' . $end . '/is'function ($r) {  
  2.     return stripslashes($r[2]);  
  3. }, $content);  

NO7和NO8,这两处是连在一起的 大约在396行,改成如下:
[php]  view plain  copy
  1. if (!$closeTag){  
  2.     $patterns       = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is';  
  3.     $replacement    = "\$this->parseXmlTag('$tagLib','$tag','$1','')";     
  4.     $content = preg_replace_callback($patternsfunction($ruse($tagLib$tag) {  
  5.         return $this->parseXmlTag($tagLib$tag$r[1], '');  
  6.     },$content);  
  7. }else{  
  8.     $patterns       = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/is';  
  9.     $replacement    = "\$this->parseXmlTag('$tagLib','$tag','$1','$2')";  
  10.     for($i=0;$i<$level;$i++)       
  11.         $content = preg_replace_callback($patterns,  function($ruse($tagLib$tag) {  
  12.             return $this->parseXmlTag($tagLib$tag$r[1], $r[2]);  
  13.         },$content);  
  14. }  

2)ThinkPHP\Lib\Core\Dispatcher.class.php
大约132行,将
[php]  view plain  copy
  1. preg_replace('@(\w+)\/([^\/]+)@e''$var[\'\\1\']=strip_tags(\'\\2\');', implode('/',$paths));  
改为
[php]  view plain  copy
  1. preg_replace_callback('@(\w+)\/([^\/]+)@',function($ruse (&$var){  
  2.     $var[$r['1']] = strip_tags($r[2]);  
  3. },implode('/',$paths));  

3)ThinkPHP\Lib\Core\Db.class.php
大约605行,将
[php]  view plain  copy
  1. $joinStr = preg_replace("/__([A-Z_-]+)__/esU",C("DB_PREFIX").".strtolower('$1')",$joinStr);  
改为
[php]  view plain  copy
  1. $DB_PREFIX = C("DB_PREFIX");  
  2. $joinStr = preg_replace_callback("/__([A-Z_-]+)__/sU",function ($ruse($DB_PREFIX) {  
  3.     return $DB_PREFIX.".strtolower({$r[1]})";  
  4. },$joinStr);  

4)ThinkPHP\Lib\Behavior\CheckRouteBehavior.class.php
这个文件需要修改的地方共4处
NO1. 大约151行,将
[php]  view plain  copy
  1. $url  =  preg_replace('/:(\d+)/e','$values[\\1-1]',$url);  
改为
[php]  view plain  copy
  1. $url = preg_replace_callback('/:(\d+)/',function($ruse (&$values){  
  2.     return $values[$r[1]-1];  
  3. },$url);  
NO2. 大约168行,将
[php]  view plain  copy
  1. preg_replace('@(\w+)\/([^\/]+)@e''$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/',$paths));  
改为
[php]  view plain  copy
  1. preg_replace_callback('@(\w+)\/([^\/]+)@',function($ruse (&$var){  
  2.     $var[strtolower($r['1'])] = strip_tags($r[2]);  
  3. },implode('/',$paths));  

NO3. 大约191行,将
[php]  view plain  copy
  1. $url = preg_replace('/:(\d+)/e','$matches[\\1]',$url);  
改为
[php]  view plain  copy
  1. $url = preg_replace_callback('/:(\d+)/'function($ruse (&$matches) {  
  2.     return $matches[$r[1]];  
  3. },$url);  

NO4. 大约201行,将
[php]  view plain  copy
  1. preg_replace('@(\w+)\/([^,\/]+)@e''$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');'$regx);  
改为
[php]  view plain  copy
  1. preg_replace_callback('@(\w+)\/([^,\/]+)@',function($ruse (&$var){  
  2.     $var[strtolower($r['1'])] = strip_tags($r[2]);  
  3. },$regx);  

5)ThinkPHP\Extend\Mode\Lite\Dispatcher.class.php
大约65行,将
[php]  view plain  copy
  1. $res = preg_replace('@(\w+)'.$depr.'([^'.$depr.'\/]+)@e''$var[\'\\1\']="\\2";', implode($depr,$paths));  
改为
[php]  view plain  copy
  1. preg_replace_callback('@(\w+)'.$depr.'([^'.$depr.'\/]+)@',function($ruse (&$var){  
  2.     $var[$r['1']] = $r[2];  
  3. },implode($depr,$paths));  

6)ThinkPHP\Lib\Behavior\ReadHtmlCacheBehavior.class.php
大约65行处的一个if判断替换为:

[php]  view plain  copy
  1. if(!empty($html)) {  
  2.     // 解读静态规则  
  3.     $rule   = $html[0];  
  4.     // 以$_开头的系统变量  
  5.     //$rule   = preg_replace('/{\$(_\w+)\.(\w+)\|(\w+)}/e',"\\3(\$\\1['\\2'])",$rule);  
  6.     $rule = preg_replace_callback('/{\$(_\w+)\.(\w+)\|(\w+)}/',function($r){  
  7.         $system = "$r[3](\${$r[1]}['$r[2]'])";  
  8.         eval"\$system = $system ;" );  
  9.         return $system;  
  10.     },$rule);  
  11.     //$rule   = preg_replace('/{\$(_\w+)\.(\w+)}/e',"\$\\1['\\2']",$rule);  
  12.     $rule = preg_replace_callback('/{\$(_\w+)\.(\w+)}/',function($r){  
  13.         $system = "\${$r[1]}['$r[2]']";  
  14.         eval"\$system = $system ;" );  
  15.         return $system;  
  16.     },$rule);  
  17.     // {ID|FUN} GET变量的简写  
  18.     //$rule   = preg_replace('/{(\w+)\|(\w+)}/e',"\\2(\$_GET['\\1'])",$rule);  
  19.     $rule = preg_replace_callback('/{(\w+)\|(\w+)}/',function($r){  
  20.         return $r[2]($_GET[$r[1]]);  
  21.     },$rule);  
  22.     //$rule   = preg_replace('/{(\w+)}/e',"\$_GET['\\1']",$rule);  
  23.     $rule = preg_replace_callback('/{(\w+)}/',function($r){  
  24.         return $_GET[$r[1]];  
  25.     },$rule);  
  26.     // 特殊系统变量  
  27.     $rule   = str_ireplace(  
  28.         array('{:app}','{:module}','{:action}','{:group}'),  
  29.         array(APP_NAME,MODULE_NAME,ACTION_NAME,defined('GROUP_NAME')?GROUP_NAME:''),  
  30.         $rule);  
  31.     // {|FUN} 单独使用函数  
  32.     //$rule  = preg_replace('/{\|(\w+)}/e',"\\1()",$rule);  
  33.     $rule  = preg_replace_callback('/{\|(\w+)}/',function($r){  
  34.         return $r[1]();  
  35.     },$rule);  
  36.     if(!empty($html[2])) $rule    =   $html[2]($rule); // 应用附加函数  
  37.     $cacheTime = isset($html[1])?$html[1]:C('HTML_CACHE_TIME'); // 缓存有效期  
  38.     // 当前缓存文件  
  39.     define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX'));  
  40.     return $cacheTime;  
  41. }  

7)ThinkPHP\Common\common.php

大约217行,将
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));

改为
$str = preg_replace_callback('/_([a-zA-Z])/', function($r) {  
            return strtoupper($r[1]);  
        }, $name);
        
        return ucfirst($str);



二、如果有用到验证码,请把ThinkPHP\Extend\Library\ORG\Util目录下的String.class.php复制一份放在一起,并改名为Stringnew.class.php

打开Stringnew.class.php

把所有的String::替换为Stringnew::

并把类名Class String 改为 Class Stringnew
打开ThinkPHP\Extend\Library\ORG\Util\Image.class.php,把所有的

[php]  view plain  copy
  1. import('ORG.Util.String');  
替换成

[php]  view plain  copy
  1. import('ORG.Util.Stringnew');  

三、自定义函数的参数最好给定默认值,如果自定义函数规定了参数,但是没有指定默认值,在外部调用的时候,如果传参的时候少传值了,那么运行会报错!


这样,在PHP7下就能运行ThinkPHP3.1了。
发布了98 篇原创文章 · 获赞 25 · 访问量 60万+

猜你喜欢

转载自blog.csdn.net/lg_lin/article/details/80298091