温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP升级7.2之后需要注意的事情有哪些

发布时间:2021-03-22 09:02:48 来源:亿速云 阅读:108 作者:小新 栏目:编程语言

这篇文章主要介绍了PHP升级7.2之后需要注意的事情有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

最近升级了PHP版本,从7.1升级到7.2,升级前版本:

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

升级后版本:

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之后废弃了一些功能,下面列出几个常见的问题:

1、each函数已被废弃:

之前版本写法:

<?php
    $array = array();
    each($array);

    // Deprecated:  The each() function is deprecated. This message will be suppressed on further calls

在7.2版本中会提示过时,可以使用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、当传递一个无效参数时,count()函数将抛出warning警告:

之前版本写法

<?php
    count('');    // Warning:  count(): Parameter must be an array or an object that implements Countable

在7.2版本中将严格执行类型区分,参数类型不正确,将会出现警告,所以需要在使用count方法时注意参数的值,不过也可以通过自己修改方法来替代(不建议):

<?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被废弃,可以用匿名函数来代替:

之前版本写法:

<?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版本中会有警告提示,可修改为匿名函数来替代:

<?php
    $newfunc = function ($a,$b){
        return "ln($a) + ln($b) = " . log($a * $b);
    };    echo $newfunc(2, M_E) . "\n";

感谢你能够认真阅读完这篇文章,希望小编分享的“PHP升级7.2之后需要注意的事情有哪些”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI