重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要介紹升級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 放在 post
的 relations
中,這樣 isset($post->user)
為 true
,隨后 isset($post->user->name)
才為 true
。
最后在 Eloquent model
的 git 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怎么辦的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!