[PHP/en] Nullsafe operator (#4248)

This commit is contained in:
ktunprasert 2021-11-01 21:20:59 +00:00 committed by GitHub
parent 8af43d446e
commit c465acbc3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -591,6 +591,12 @@ echo $my_class->property; // => "public"
echo $my_class->instanceProp; // => "An instance property" echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod(); // => "MyClass" $my_class->myMethod(); // => "MyClass"
// Nullsafe operators since PHP 8
// You can use this when you're unsure if the abstraction of $my_class contains has a property/method
// it can be used in conjunction with the nullish coalesce operator to ensure proper value
echo $my_class->invalid_property // An error is thrown
echo $my_class?->invalid_property // => NULL
echo $my_class?->invalid_property ?? "public" // => "public"
// Extend classes using "extends" // Extend classes using "extends"
class MyOtherClass extends MyClass class MyOtherClass extends MyClass