最近升級了PHP版本,從7.1升級到7.2,升級前版本:php
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
升級後版本:ruby
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
升級完成以後發現有幾個框架在使用時都出現了問題,主要緣由集中在7.2以後廢棄了一些功能,下面列出幾個常見的問題:框架
以前版本寫法:函數
<?php
$array = array(); each($array); // Deprecated: The each() function is deprecated. This message will be suppressed on further calls
在7.2版本中會提示過期,能夠使用foreach替代each方法,也能夠本身修改each方法替代:ui
<?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; }
以前版本寫法this
<?php
count(''); // Warning: count(): Parameter must be an array or an object that implements Countable
在7.2版本中將嚴格執行類型區分,參數類型不正確,將會出現警告,因此須要在使用count方法時注意參數的值,不過也能夠經過本身修改方法來替代(不建議):spa
<?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; } }
以前版本寫法:debug
<?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版本中會有警告提示,可修改成匿名函數來替代:code
<?php $newfunc = function ($a,$b){ return "ln($a) + ln($b) = " . log($a * $b); }; echo $newfunc(2, M_E) . "\n";