日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

thinkphp3.1迁移php7,ThinkPHP3.1迁移到PHP7的注意事项

發布時間:2024/4/14 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 thinkphp3.1迁移php7,ThinkPHP3.1迁移到PHP7的注意事项 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ThinkPHP3.1遷移到PHP7的注意事項

2018.10.31

3723

小平

ThinkPHP3.1遷移到PHP7的注意事項

最近項目從PHP5.5升級到了PHP7.0,框架是ThinkPHP3.1.3,記錄下升級過程。

一、我用的apache服務器,項目里開啟了路由功能,所以.htaccess文件就改成了

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]

/eis',"\$this->restoreLiteral('\\1')",$tmplContent);

替換為

$tmplContent = preg_replace_callback('//is',function($r) {

return$this->restoreLiteral($r[1]);

}, $tmplContent);

NO2. 大約在168行,將

$content = preg_replace('/'.$begin.'literal'.$end.'(.*?)'.$begin.'\/literal'.$end.'/eis',"\$this->parseLiteral('\\1')",$content);

替換為

$content = preg_replace_callback('/'. $begin .'literal'. $end .'(.*?)'. $begin .'\/literal'. $end .'/is',function($r) {

return$this->parseLiteral($r[1]);

}, $content);

NO3. 大約在197行,將

$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);

替換為

$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) {

return$this->parseTag($r[2]);

}, $content);

NO4. 大約在266行,將

preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"\$this->parseBlock('\\1','\\2')",$content);

替換為

preg_replace_callback('/'. $begin .'block\sname=(.+?)\s*?'. $end .'(.*?)'. $begin .'\/block'. $end .'/is',function($r) {

$this->parseBlock($r[1], $r[2]);

}, $content);

NO5. 大約在271行,將

$content = preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"\$this->replaceBlock('\\1','\\2')",$content);

替換為

$content = preg_replace_callback('/'. $begin .'block\sname=(.+?)\s*?'. $end .'(.*?)'. $begin .'\/block'. $end .'/is',function($r) {

return$this->replaceBlock($r[1], $r[2]);

}, $content);

NO6. 大約在273行,將

$content = preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"stripslashes('\\2')",$content);

替換為

$content = preg_replace_callback('/'. $begin .'block\sname=(.+?)\s*?'. $end .'(.*?)'. $begin .'\/block'. $end .'/is',function($r) {

returnstripslashes($r[2]);

}, $content);

NO7和NO8,這兩處是連在一起的 大約在396行,改成如下:

if(!$closeTag){

$patterns ??????='/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is';

$replacement ???="\$this->parseXmlTag('$tagLib','$tag','$1','')";

$content = preg_replace_callback($patterns,function($r)use($tagLib, $tag) {

return$this->parseXmlTag($tagLib, $tag, $r[1],'');

},$content);

}else{

$patterns ??????='/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/is';

$replacement ???="\$this->parseXmlTag('$tagLib','$tag','$1','$2')";

for($i=0;$i

$content = preg_replace_callback($patterns,function($r)use($tagLib, $tag) {

return$this->parseXmlTag($tagLib, $tag, $r[1], $r[2]);

},$content);

}

2)ThinkPHP\Lib\Core\Dispatcher.class.php大約132行,將

preg_replace('@(\w+)\/([^\/]+)@e','$var[\'\\1\']=strip_tags(\'\\2\');', implode('/',$paths));

改為

preg_replace_callback('@(\w+)\/([^\/]+)@',function($r)use(&$var){

$var[$r['1']] = strip_tags($r[2]);

},implode('/',$paths));

3)ThinkPHP\Lib\Core\Db.class.php大約605行,將

$joinStr = preg_replace("/__([A-Z_-]+)__/esU",C("DB_PREFIX").".strtolower('$1')",$joinStr);

改為

$DB_PREFIX = C("DB_PREFIX");

$joinStr = preg_replace_callback("/__([A-Z_-]+)__/sU",function($r)use($DB_PREFIX) {

return$DB_PREFIX.".strtolower({$r[1]})";

},$joinStr);

4)ThinkPHP\Lib\Behavior\CheckRouteBehavior.class.php這個文件需要修改的地方共4處NO1. 大約151行,將

$url ?= ?preg_replace('/:(\d+)/e','$values[\\1-1]',$url);

改為

$url = preg_replace_callback('/:(\d+)/',function($r)use(&$values){

return$values[$r[1]-1];

},$url);

NO2. 大約168行,將

preg_replace('@(\w+)\/([^\/]+)@e','$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/',$paths));

改為

preg_replace_callback('@(\w+)\/([^\/]+)@',function($r)use(&$var){

$var[strtolower($r['1'])] = strip_tags($r[2]);

},implode('/',$paths));

NO3. 大約191行,將

$url = preg_replace('/:(\d+)/e','$matches[\\1]',$url);

改為

$url = preg_replace_callback('/:(\d+)/',function($r)use(&$matches) {

return$matches[$r[1]];

},$url);

NO4. 大約201行,將

preg_replace('@(\w+)\/([^,\/]+)@e','$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx);

改為

preg_replace_callback('@(\w+)\/([^,\/]+)@',function($r)use(&$var){

$var[strtolower($r['1'])] = strip_tags($r[2]);

},$regx);

5)ThinkPHP\Extend\Mode\Lite\Dispatcher.class.php大約65行,將

$res = preg_replace('@(\w+)'.$depr.'([^'.$depr.'\/]+)@e','$var[\'\\1\']="\\2";', implode($depr,$paths));

改為

preg_replace_callback('@(\w+)'.$depr.'([^'.$depr.'\/]+)@',function($r)use(&$var){

$var[$r['1']] = $r[2];

},implode($depr,$paths));

6)ThinkPHP\Lib\Behavior\ReadHtmlCacheBehavior.class.php大約65行處的一個if判斷替換為:

if(!empty($html)) {

// 解讀靜態規則

$rule ??= $html[0];

// 以$_開頭的系統變量

//$rule ??= preg_replace('/{\$(_\w+)\.(\w+)\|(\w+)}/e',"\\3(\$\\1['\\2'])",$rule);

$rule = preg_replace_callback('/{\$(_\w+)\.(\w+)\|(\w+)}/',function($r){

$system ="$r[3](\${$r[1]}['$r[2]'])";

eval("\$system = $system ;");

return$system;

},$rule);

//$rule ??= preg_replace('/{\$(_\w+)\.(\w+)}/e',"\$\\1['\\2']",$rule);

$rule = preg_replace_callback('/{\$(_\w+)\.(\w+)}/',function($r){

$system ="\${$r[1]}['$r[2]']";

eval("\$system = $system ;");

return$system;

},$rule);

// {ID|FUN} GET變量的簡寫

//$rule ??= preg_replace('/{(\w+)\|(\w+)}/e',"\\2(\$_GET['\\1'])",$rule);

$rule = preg_replace_callback('/{(\w+)\|(\w+)}/',function($r){

return$r[2]($_GET[$r[1]]);

},$rule);

//$rule ??= preg_replace('/{(\w+)}/e',"\$_GET['\\1']",$rule);

$rule = preg_replace_callback('/{(\w+)}/',function($r){

return$_GET[$r[1]];

},$rule);

// 特殊系統變量

$rule ??= str_ireplace(

array('{:app}','{:module}','{:action}','{:group}'),

array(APP_NAME,MODULE_NAME,ACTION_NAME,defined('GROUP_NAME')?GROUP_NAME:''),

$rule);

// {|FUN} 單獨使用函數

//$rule ?= preg_replace('/{\|(\w+)}/e',"\\1()",$rule);

$rule ?= preg_replace_callback('/{\|(\w+)}/',function($r){

return$r[1]();

},$rule);

if(!empty($html[2])) $rule ???= ??$html[2]($rule);// 應用附加函數

$cacheTime =isset($html[1])?$html[1]:C('HTML_CACHE_TIME');// 緩存有效期

// 當前緩存文件

define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX'));

return$cacheTime;

}

四、如果有用到驗證碼,請把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,把所有的

import('ORG.Util.String');

替換成

import('ORG.Util.Stringnew');

五、自定義函數的參數最好給定默認值,如果自定義函數規定了參數,但是沒有指定默認值,在外部調用的時候,如果傳參的時候少傳值了,那么運行會報錯!

這樣,在PHP7下就能運行ThinkPHP3.1了。

分享到:

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的thinkphp3.1迁移php7,ThinkPHP3.1迁移到PHP7的注意事项的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。