ホームページ  >  記事  >  バックエンド開発  >  php7にアップグレード後、issetメソッドが常にfalseになる問題を解決

php7にアップグレード後、issetメソッドが常にfalseになる問題を解決

藏色散人
藏色散人転載
2019-03-29 17:15:342945ブラウズ

会社が php7 をアップグレードした後に、これと同様の問題が発生しました。 isset($post->user->name) は常に false、以前の php 5.6 は正常でした、laravelのバージョンは5.1.35です(長い間アップグレードされていません)。

#まず見てみましょうisset

##isset

は、変数が設定されているかどうかを検出するために使用されますまずはやってみましょう。公式の例

を見てみましょう。これは大まかに次のような意味です

<?php

class Post
{
    protected $attributes = [&#39;content&#39; => &#39;foobar&#39;];

    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 = [&#39;content&#39; => &#39;foobar&#39;];

    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のコードを見て、自分たちで簡単な例を書きます

最初のA

モデル

、簡単な実装。 __get__set__isset<pre class="brush:php;toolbar:false">class Model { // 存放属性 protected $attributes = []; // 存放关系 protected $relations = []; public function __get($key) { if( isset($this-&gt;attributes[$key]) ) { return $this-&gt;attributes[$key]; } // 找到关联的对象,放在关系里面 if (method_exists($this, $key)) { $relation = $this-&gt;$method(); return $this-&gt;relations[$method] = $relation; } } public function __set($k, $v) { $this-&gt;attributes[$k] = $v; } public function __isset($key) { if (isset($this-&gt;attributes[$key]) || isset($this-&gt;relations[$key])) { return true; } return false; } }</pre> 次に、

Post Moel

User Moel# を定義します。 ##

class Post extends Model
{

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

}

class User extends Model
{
}
さて、検証してみましょうisset

$post = new Post();

echo &#39;isset 发帖用户:&#39;;
echo isset($post->user) ? &#39;true&#39; : &#39;false&#39;;  // false
echo PHP_EOL;

echo &#39;isset 发帖用户的名字:&#39;;
echo isset($post->user->name) ? &#39;true&#39; : &#39;false&#39;;  // false
echo PHP_EOL;

echo &#39;发帖用户的名字:&#39;;
echo $post->user->name;    // user name
echo PHP_EOL;

echo &#39;再次判断 isset 发帖用户的名字:&#39;;
echo isset($post->user->name) ? &#39;true&#39; : &#39;false&#39;;   // true
echo PHP_EOL;
回答

上記の結果を分析すると、php のような感じです。 7 isset

メソッドはオブジェクトの判定を変更しました。最初に 1 回実行すると、

$post->user->name、つまり user が # に配置されます。 ##postrelations (isset($post->user) true であり、その後 isset($ post->user->name) true です。 #Eloquent モデル 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&#39;s comment (core PHP dev) - 
https://bugs.php.net/bug.php?id=69659

は、おおよそ php7 が判定を設定すると、順番に判定します。 php5.6 ではリレーションシップがプリロードされます。実は、laravelではすでに関連する処理が行われているので、laravelをバージョンアップすれば、この問題は自然に解消されます。

以上がphp7にアップグレード後、issetメソッドが常にfalseになる問題を解決の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はlearnku.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。