晋级 php7 后 isset 没有太对于了
私司晋级 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 的例子
望着 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 co妹妹ent (core PHP dev) - https://bugs.php.net/bug.php必修id=69659
小致上是 php7 isset 剖断的时辰,会挨次断定。php5.6 则会预添载关连。其真 laravel 也晚正在 5 月份便作了相闭的处置惩罚,以是晋级 laravel 后,天然也便不那个答题了。
引荐学程:《PHP7学程》《PHP学程》《Laravel学程》
以上便是PHP7外的isset的具体形式,更多请存眷萤水红IT仄台其余相闭文章!

发表评论 取消回复