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动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题