php新手用7.2,总结PHP升级7.2之后需要注意的事情
最近升級(jí)了PHP版本,從7.1升級(jí)到7.2,升級(jí)前版本:PHP 7.1.14 (cli) (built: Feb 2 2018 08:42:59) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.1.14, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans
升級(jí)后版本:PHP 7.2.2 (cli) (built: Feb 24 2018 17:51:12) ( ZTS DEBUG ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.2, Copyright (c) 1999-2018, by Zend Technologies
推薦(免費(fèi)):PHP7
升級(jí)完成之后發(fā)現(xiàn)有幾個(gè)框架在使用時(shí)都出現(xiàn)了問題,主要原因集中在7.2之后廢棄了一些功能,下面列出幾個(gè)常見的問題:
1、each函數(shù)已被廢棄:
之前版本寫法:<?php
$array = array();
each($array);
// Deprecated: The each() function is deprecated. This message will be suppressed on further calls
在7.2版本中會(huì)提示過時(shí),可以使用foreach替代each方法,也可以自己修改each方法替代:<?php
function func_new_each(&$array){
$res = array(); $key = key($array); if($key !== null){
next($array);
$res[1] = $res['value'] = $array[$key]; $res[0] = $res['key'] = $key;
}else{ $res = false;
} return $res;
}
2、當(dāng)傳遞一個(gè)無效參數(shù)時(shí),count()函數(shù)將拋出warning警告:
之前版本寫法<?php
count(''); // Warning: count(): Parameter must be an array or an object that implements Countable
在7.2版本中將嚴(yán)格執(zhí)行類型區(qū)分,參數(shù)類型不正確,將會(huì)出現(xiàn)警告,所以需要在使用count方法時(shí)注意參數(shù)的值,不過也可以通過自己修改方法來替代(不建議):<?php
function func_new_count($array_or_countable,$mode = COUNT_NORMAL){
if(is_array($array_or_countable) || is_object($array_or_countable)){ return count($array_or_countable, $mode);
}else{ return 0;
}
}
3、create_function被廢棄,可以用匿名函數(shù)來代替:
之前版本寫法:<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);'); echo "New anonymous function: $newfunc\n"; echo $newfunc(2, M_E) . "\n"; // outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
// Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
在7.2版本中會(huì)有警告提示,可修改為匿名函數(shù)來替代:<?php
$newfunc = function ($a,$b){
return "ln($a) + ln($b) = " . log($a * $b);
}; echo $newfunc(2, M_E) . "\n";
以上就是升級(jí)之后暫時(shí)遇到的幾個(gè)問題,其它相關(guān)修改可詳看鏈家產(chǎn)品技術(shù)團(tuán)隊(duì)做的翻譯及整理:PHP7.2 版本指南
總結(jié)
以上是生活随笔為你收集整理的php新手用7.2,总结PHP升级7.2之后需要注意的事情的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年净利润542.6亿元:茅台5年
- 下一篇: 动态规划算法php,php算法学习之动态