xpuc7o xpuc7o - 1 month ago 5
PHP Question

Why __isset() is not triggered in Laravel Model?

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?

Answer

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