Skip to content

Null Coalescing Operator with objects' attributes #3283

@ThibaudDauce

Description

@ThibaudDauce

Bug report

https://phpstan.org/r/d7f7272b-92ac-4497-a6b2-07c98e3df70e

<?php declare(strict_types = 1);

$test = random_int(0, 10);

if ($test > 5) {
	$user = new stdClass;
	$user->name = 'Thibaud';
}

echo $user->name ?? 'Default'; // Variable $user might not be defined.

Expected output

There is no error in this code, PHP isset is working with nested attributes isset($abc->def->ghi) (https://www.php.net/manual/fr/function.isset.php#86313) and $var ?? $default is only a shortcut to isset($var) ? $var : $default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions