解決升級php7後isset方法始終為 false的問題
公司升級php7 後出現了一個問題,類似這樣 isset($post->user->name)
總是為 false
,之前的php 5.6 就很正常,laravel 版本是5.1.35(很久沒升級了)。
先看看isset
isset
用來偵測變數是否設定
首先我們來看官方的例子
大致上是下面這個意思
<?php class Post { protected $attributes = ['content' => '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
<?PHP class Post { protected $attributes = ['content' => '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 的範例
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
。 最後在
的 以上是解決升級php7後isset方法始終為 false的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!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

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

解決 PHP 7.0 中插件未顯示已安裝問題的方法:檢查插件配置並啟用插件。重新啟動 PHP 以套用配置變更。檢查插件檔案權限,確保其正確。安裝遺失的依賴項,以確保插件正常運作。如果其他步驟都失敗,則重建 PHP。其他可能原因包括外掛程式版本不相容、載入錯誤版本或 PHP 配置問題。

php7.0安裝mongo擴充的方法:1、建立mongodb使用者群組和使用者;2、下載mongodb原始碼包,並將原始碼包放到“/usr/local/src/”目錄下;3、進入“src/”目錄;4、解壓縮原始碼包;5、建立mongodb檔案目錄;6、將檔案複製到「mongodb/」目錄;7、建立mongodb設定檔並修改設定即可。

在php5中,我們可以使用fsockopen()函數來偵測TCP埠。這個函數可以用來開啟一個網路連接和進行一些網路通訊。但是在php7中,fsockopen()函數可能會遇到一些問題,例如無法開啟連接埠、無法連接到伺服器等。為了解決這個問題,我們可以使用socket_create()函數和socket_connect()函數來偵測TCP埠。

這篇文章將為大家詳細講解有關PHP判斷某個數組中是否存在指定的key,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP判斷某個陣列中是否存在指定的key:在php中,判斷某個陣列中是否存在指定的key的方法有多種:1.使用isset()函數:isset($array["key"])此函數傳回布林值,如果指定的key存在,則傳回true,否則傳回false。 2.使用array_key_exists()函數:array_key_exists("key",$arr

PHP伺服器環境常見的解決方法包括:確保已安裝正確的PHP版本和已複製相關檔案到模組目錄。暫時或永久停用SELinux。檢查並配置PHP.ini,確保已新增必要的擴充功能和進行正確設定。啟動或重新啟動PHP-FPM服務。檢查DNS設定是否有解析問題。

php7.0安裝部署的方法:1、到PHP官網下載與本機系統對應的安裝版本;2、將下載的zip檔案解壓縮到指定目錄;3、開啟命令列窗口,在「E:\php7」目錄下運行“php -v”命令即可。

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

在Docker環境中使用PECL安裝擴展時報錯的原因及解決方法在使用Docker環境時,我們常常會遇到一些令人頭疼的問�...
