Voting

: min(five, three)?
(Example: nine)

The Note You're Voting On

Trismegiste
2 years ago
Please be aware of hidden behaviors with uninitialised properties. The note explains : « Uninitialized properties are considered inaccessible, and thus will not be included in the array. » but that's not entirely true in PHP 8.1. It depends if the property is type-hinted or not.

<?php

class Example
{
public
$untyped;
public
string $typedButNotInitialized;
public ?
string $typedOrNullNotInitialized;
public ?
string $typedOrNullWithDefaultNull = null;
}

var_dump(get_object_vars(new Example()));
?>

will print :

array(2) {
["untyped"]=>
NULL
["typedOrNullWithDefaultNull"]=>
NULL
}

As you can see, only "untyped" and "typedOrNullWithDefaultNull" properties are dumped with get_object_vars(). You may encounter problems when migrating old source code and adds carelessly types everywhere without proper initialisation (or default) and assuming it defaults to NULL like old code does.

Hope this helps

<< Back to user notes page

To Top