duanhao4156 2016-10-25 15:56
浏览 307
已采纳

为什么在Laravel模型中没有触发__isset()?

I have a Laravel project with this code:

$names = isset($user->party->personName->first_name) ? $user->party->personName->first_name . ' ' . $user->party->personName->last_name : null;

$role = $user->userRole->role->name ?? null

On the dev server this is working. On Homestead $names and $role are always null. As I know isset() should trigger __isset() method in the Model class but id does not.

Laravel version - 5.2.31

PHP on dev server - 7.0.2

PHP on Homestead - 7.0.8-2+deb.sury.org~xenial+1

Is this because the difference of the PHP versions or there is some setting?

  • 写回答

2条回答 默认 最新

  • doudou1897 2016-10-25 16:11
    关注

    The behaviour changed in PHP 7.0.6. You can reproduce a basic example using the following:

    class Foo {
      public function __isset($arg) {
        echo '__isset called', PHP_EOL;
      }
    }
    
    $foo = new Foo;
    
    echo 'Basic:', PHP_EOL;
    isset($foo->property);
    
    echo 'Deep:', PHP_EOL;
    isset($foo->property->child);
    

    Before 7.0.6, the __isset method was only called for the top-level property, whereas now it's called for the deeper, child property as well.

    See https://3v4l.org/D389q for the different results.

    It looks to stem back to a fix applied for this bug: https://bugs.php.net/bug.php?id=69659

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂