老熟女激烈的高潮_日韩一级黄色录像_亚洲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
主站蜘蛛池模板: 日本强好片久久久久久aaa | 性刺激的大陆三级视频 | 成人日韩在线 | 亚洲成国产人片在线观看 | 成人三区四区 | 国产精品网红尤物福利在线观看 | 精品一二三区视频 | 日韩av最新网址 | 91精品国产成人 | 黄色av免费在线看 | 欧美高清FREEXXXX性 | 91porny九色91啦中文 | 日本欧美一区二区三区在线观看 | 亚洲熟妇另类久久久久久 | 午夜免费观看 | 日本一区二区三区视频在线播放 | 一本久久a久久精品综合 | 亚洲国产一区二区三区精品 | 亚洲阿v天堂无码z2018 | 国产伦理精品 | 精品成人佐山爱一区二区 | 成年人射精的黄色视频网站 | 亚洲精品国产高清一线久久 | 国产一级特黄录像片 | 热久久中文字幕 | 欧美狠狠插 | 久草免费资源站 | 久久久久99精品久久久久 | 久草院线| 国产精品国产三级国产普通 | 欧美XXXX18性欧美 | 性少妇xxxxx 少妇与大狼拘作爱视频 | 757福利视频 | 国产一级特黄真人毛片 | 麻豆资源网 | 国产成人一区二区三区在线 | 国产亚洲精品无码专区 | 国产区在线看 | 中文字幕二区在线 | 国产精品一区二区免费看 | 伊人久久大线影院首页 |