老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

升級php7后isset方法始終為false怎么辦

這篇文章主要介紹升級php7后isset方法始終為 false怎么辦,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創新互聯公司服務項目包括滎陽網站建設、滎陽網站制作、滎陽網頁制作以及滎陽網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,滎陽網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到滎陽省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!

公司升級 php7 后出現了一個問題,類似這樣 isset($post->user->name) 始終為 false,之前的php 5.6 就很正常,laravel 版本是 5.1.35(很久沒升級了)。

先看看isset

isset 用來檢測變量是否設置

首先我們來看官方的一個例子

大致上是下面這個意思

 'foobar'];

    public function __get($key)
    {
        if (isset($this->attributes[$key])) {
            return $this->attributes[$key];
        }
    }
}

$post = new Post();
echo isset($post->content);  // false

上面這個例子將永遠返回 false,因為 foo 并不是 Post 的屬性,而是 __get 取出來的

魔術方法__isset

那么怎么解決上面那個問題呢?使用魔術方法

 'foobar'];

    public function __get($key)
    {
        if (isset($this->attributes[$key])) {
            return $this->attributes[$key];
        }
    }

    public function __isset($key)
    {
        if (isset($this->attributes[$key])) {
            return true;
        }

        return false;
    }
}

$post = new Post();
echo isset($post->content);   //true

類似 Eloquent 的例子

看著 laravel 5.1.35 的代碼,我們自己寫一個簡單的例子

先有一個 Model,簡單的實現。__get__set__isset

class Model
{
    // 存放屬性
    protected $attributes = [];

    // 存放關系
    protected $relations = [];

    public function __get($key)
    {
        if( isset($this->attributes[$key]) ) {
            return $this->attributes[$key];
        }

          // 找到關聯的對象,放在關系里面
        if (method_exists($this, $key)) {

              $relation = $this->$method();   

              return $this->relations[$method] = $relation;
        }
    }

    public function __set($k, $v)
    {
        $this->attributes[$k] = $v;
    }

    public function __isset($key)
    {
        if (isset($this->attributes[$key]) || isset($this->relations[$key])) {
            return true;
        }

        return false;
    }
}

然后我們定義一個 Post Moel 和一個 User Moel

class Post extends Model
{

    protected function user()
    {
        $user = new User();
        $user->name = 'user name';
        return $user;
    }

}

class User extends Model
{
}

好了來驗證一下isset

$post = new Post();

echo 'isset 發帖用戶:';
echo isset($post->user) ? 'true' : 'false';  // false
echo PHP_EOL;

echo 'isset 發帖用戶的名字:';
echo isset($post->user->name) ? 'true' : 'false';  // false
echo PHP_EOL;

echo '發帖用戶的名字:';
echo $post->user->name;    // user name
echo PHP_EOL;

echo '再次判斷 isset 發帖用戶的名字:';
echo isset($post->user->name) ? 'true' : 'false';   // true
echo PHP_EOL;

答案

分析上面的結果,感覺像是 php 7 isset 方法對對象的判斷有了變化,如果先執行一次,$post->user->name,也就是將 user 放在 postrelations 中,這樣 isset($post->user) true,隨后 isset($post->user->name) 才為 true

最后在 Eloquent modelgit log 中 找到了答案,

PHP 7 has fixed a bug with __isset which affects both the 
native isset and empty methods. This causes specific issues 
with checking isset or empty on relations in Eloquent. In 
PHP 7 checking if a property exists on an unloaded relation, 
for example isset($this->relation->id) is always 
returning false because unlike PHP 5.6, PHP 7 is now 
checking the offset of each attribute before chaining to 
the next one. In PHP 5.6 it would eager load the relation 
without checking the offset. This change brings back the 
intended behavior of the core Eloquent model __isset method 
for PHP 7 so it works like it did in PHP 5.6.

For reference, please check the following link, 
specifically Nikita Popov's comment (core PHP dev) - 
https://bugs.php.net/bug.php?id=69659

大致上是 php7 isset 判斷的時候,會依次判斷。php5.6 則會預加載關系。其實 laravel 也早就做了相關的處理,所以升級 laravel 后,自然也就沒有這個問題了。

以上是升級php7后isset方法始終為 false怎么辦的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!


文章題目:升級php7后isset方法始終為false怎么辦
文章網址:http://www.xueling.net.cn/article/jjhjgp.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 一级黄色免费观看视频 | 国产精品一级毛片无码老人 | 日韩欧美激情在线 | 一级爰片 | 国产在线无码一区二区三区 | 日本最新中文字幕 | 欧美成人精品A∨在线观看 啪啪av网站 | 字幕专区码中文欧美在线 | 久久久久久久久淑女av国产精品 | 日日摸日日碰夜夜爽视频 | 一区二区在线视频播放 | 网色国产| 久久九九视频 | 欧美视频网 | 兔费国产一级av片高潮版 | 色综合久久综合中文综合网 | 精品亚洲国产视频 | 色综合久久88色综合天天6 | 99精品国产aⅴ | 四虎影城库 | 久久成人国产视频 | 欧美精品久久一区二区三区 | 99精品视屏| 91在线影院| 嘿咻嘿咻免费区在线观看 | 性欧美vr高清极品 | 日本国产一区二区三区 | 久久66热人妻偷产国产 | 国产精品毛片一区二区三区四区 | 国产激情网站 | 国产毛片久久久久久 | 96国产精品 | 久久ww | 亚洲日本无码一区二区三区 | 私人影院性盈盈影院 | a日韩在线| 99热99精品 | 亮剑第二部免费观看 | 美女一区二区三区视频 | 99在线视频免费观看 | 无码人妻精品一区二区三区不卡 |