老熟女激烈的高潮_日韩一级黄色录像_亚洲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
主站蜘蛛池模板: m麻豆传媒映画 | 四虎1515永久免费 | 后入内射国产一区二区 | 色av永久无码av影院 | 亚洲国产精品人人做人人爱 | 日韩在线观看a | 国产特级片| 一区二区无码免费视频网站 | 亚洲国产精品久久人人爱蜜臀 | 久久丝袜视频 | 国产视频二区三区 | 亚洲成人中文 | 精品久久久一二三区 | 国产51人人成人人人人爽色哟哟 | 99久久久国产精品免费99 | 四川少妇BBBB槡BBBB槡 | JIZZJIZZ国产| 女高中生边自慰边呻吟 | 欧美成人天天综合在线视色 | 日本一区视频 | 亚洲成人毛片 | 免费观看全黄做爰的视在线观看 | 艳妇荡女的呻吟干柴烈火视频 | 亚洲va中文字幕无码 | 日韩1页 | 日本成人在线免费观看 | 高清在线一区二区 | 中文字幕人成无码人妻 | 狠狠夜夜 | 日韩一级片黄色 | 亚洲黄色片在线观看 | 精产国品一二三产区区别麻豆 | 91精品国产色综合久久不卡98口 | 久久97精品久久久久久久不卡 | 国产成人aa在线视频 | 国内午夜国产精品小视频 | 波多野结衣中文AV无码专区 | 欧美日本另类 | 日韩高清中文字幕一区二区 | 亚洲AV永久中文无码精品 | 特级毛片在线播放 |