栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

Laravel5.2 php环境升级到php7.2

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Laravel5.2 php环境升级到php7.2

myproject升级到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:iProjectmyprojectvendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.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;
    }

我打印 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 CategoryCron
count(): Parameter must be an array or an object that implements Countab

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

int(67)string(68) "F:iProjectmyprojectvendorguzzlehttpguzzlesrcHandlerCurlFactory.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一样。

改框架不好,还是在业务层面修改

改框架的问题就是。当你有天(或者别人)。composer update的时候。旧的框架代码移除,新的覆盖。你的修改又都没了。

改了框架并没有避免NULL传入。所以要在自己写的代码层面去控制。

再看问题2

报错的调用trace

in Builder.php line 772at HandleExceptions->handleError('2', 'count(): Parameter must be an array or an object that implements Countable', 'F:iProjectmyprojectvendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.php', '772', array('query' => object(Builder), 'boolean' => 'and'))
at count(null) in Builder.php line 772at Builder->addNestedWhereQuery(object(Builder), 'and') in Builder.php line 778

我们看谁调用了 addNestedWhereQuery,vendorlaravelframeworksrcIlluminateDatabaseEloquentBuilder.php里面的 where 方法调用 了$this->query->addNestedWhereQuery($query->getQuery(), $boolean);
也就是说 调用where方法要传一个query->getQuery(),还不能为null。

再看看业务代码,找到一些类似如下的代码

->where(function ($query) use ($condition) {     if (!empty($condition['title'])) {
         $query->where('page.title', 'like', '%' . $condition['title'] . '%');
     }
})
->where(function ($query) use ($condition) {     if (!empty($condition['author'])) {
         $query->where('page.author', 'like', '%' . $condition['author'] . '%');
     } 
})

看完之后,就明白了。原因了。没有传条件的时候,是没有$query->where()。所以就为NULL。

修正,多个where 合并到一个where即可。再加一行代码,  $query->where('page.id', '>' . 0');(这不是一个很好的解决方式,但是先让代码跑起来。当然我们知道了原因,怎么做有很多方式了,对吧)

->where(function ($query) use ($condition) {     if (!empty($condition['title'])) {
         $query->where('page.title', 'like', '%' . $condition['title'] . '%');
     }    if (!empty($condition['author'])) {
         $query->where('page.author', 'like', '%' . $condition['author'] . '%');
     } 
    $query->where('page.id', '>' . 0');
})
再看问题3

网上有人遇到这个问题。Crash CurlFactory:67 - PHP 7.2.0-dev

现在的6.3 版本是没问题的。因为框架默认给了$handles一个[]。

class CurlFactory implements CurlFactoryInterface{    
    private $handles;

变成了如下。

class CurlFactory implements CurlFactoryInterface{    
    private $handles = [];

单独 composer update guzzlehttp/guzzle遇到了一个小问题,意思是不能单独升级guzzle,其他依赖版本不升也不行。

所以我们可以思考一个问题。当我们写代码,声明属性的时候。如果是数组那还是初始化一个[],否则某天方法中count就会报错了。



作者:言十年
链接:https://www.jianshu.com/p/1fd2df264c2e


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/226955.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号