【轻知识】Laravel5.2更新到php7.2

作者 : 开心源码 本文共2246个字,预计阅读时间需要6分钟 发布时间: 2022-05-12 共246人阅读

更新到php7.2

laravel 5.2。本地开发环境php7.1,线上php7.0。要把php7.0更新到php7.2。

目前遇到的问题就是each、count 这两个。

1.each 废弃2.count 只能 countable type 的变量

以下是遇到少量小问题。做个记录。

1. ModulesServiceProvider.php (这个是我自己的文件) The each() function is deprecated. This message will be suppressed on further calls

while (list(, $module) = each($modules)) {            // Load the routes for each of the modules            if (file_exists(__DIR__ . '/' . $module . '/routes.php')) {                include __DIR__ . '/' . $module . '/routes.php';            }            // Load the views            if (is_dir(__DIR__ . '/' . $module . '/Views')) {                $this->loadViewsFrom(__DIR__ . '/' . $module . '/Views', $module);            }        }

php 7.2 废弃了each 。while(…each..)改成foreah 就可

修改为

foreach ($modules as $module) {            // Load the routes for each of the modules            if (file_exists(__DIR__ . '/' . $module . '/routes.php')) {                include __DIR__ . '/' . $module . '/routes.php';            }            // Load the views            if (is_dir(__DIR__ . '/' . $module . '/Views')) {                $this->loadViewsFrom(__DIR__ . '/' . $module . '/Views', $module);            }        }

2. ErrorException in Builder.php line 772:

count(): Parameter must be an array or an object that implements Countable

问题定位到了。in Builder.php line 772
at HandleExceptions->handleError(‘2’, ‘count(): Parameter must be an array or an object that implements Countable’, ‘F:\iProject\afp\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php’, ‘772’, array(‘query’ => object(Builder), ‘boolean’ => ‘and’))

问题源

    public function addNestedWhereQuery($query, $boolean = 'and')    {        if (count($query->wheres)) {            $type = 'Nested';            $this->wheres[] = compact('type', 'query', 'boolean');            $this->addBinding($query->getBindings(), 'where');        }        return $this;    }

我打印 query->wheres 时候,发现query->wheres 为 NULL。count(NULL) 在其余版本结果为 0.

修改为

if ($query->wheres != NULL && count($query->wheres)) {            $type = 'Nested';            $this->wheres[] = compact('type', 'query', 'boolean');            $this->addBinding($query->getBindings(), 'where');        }

但是这样就动了框架了。我以为新框架会对这段做改变。我之后下载了laravel 5.7的框架。这段代码没变。

3.当我开始测脚本的时候

$ APP_ENV="test" php72 artisan CategoryCroncount(): Parameter must be an array or an object that implements Countab

定位。在解决异常的地方打印行数与文件

int(67)string(68) "F:\iProject\afp\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php"

找到代码

public function release(EasyHandle $easy)    {        $resource = $easy->handle;        unset($easy->handle);        if (count($this->handles) >= $this->maxHandles) {            curl_close($resource);

跟上面一样。$this->handles为null所以才报错了。兼容方式跟问题2一样。

参考:

*《php 7.2 少量注意事项.,php7.2注意事项》http://www.bkjia.com/PHPjc/1235440.html
*《从PHP 7.1.x 移植到 PHP 7.2.x》http://php.net/manual/zh/migration72.php 可以看官网文档理解下,新特性以及兼容等等

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 【轻知识】Laravel5.2更新到php7.2

发表回复