xpuc7o
xpuc7o

Reputation: 333

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?

Upvotes: 0

Views: 625

Answers (2)

iainn
iainn

Reputation: 17417

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

Upvotes: 4

JOUM2
JOUM2

Reputation: 1

__isset() is called when you are do isset() on an object, when __isset() is defined in the class.

make a debug_print_backtrace in the __isset method, to see if its really called

But what if any of the selected variables before tha last isnt set?

isset($user->party->personName->first_name)

Upvotes: -1

Related Questions