note 127940 added to function.get-object-vars
| From: | Trismegiste at main dot php dot net | Date: | Wed, 16 Nov 2022 15:34:21 +0000 |
| Subject: | note 127940 added to function.get-object-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219894@lists.php.net to get a copy of this message | ||
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
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 185.83.236.219)
----
Manual Page -- https://php.net/manual/en/function.get-object-vars.php
Edit -- https://main.php.net/note/edit/127940
Del: integrated -- https://main.php.net/note/delete/127940/integrated
Del: useless -- https://main.php.net/note/delete/127940/useless
Del: bad code -- https://main.php.net/note/delete/127940/bad+code
Del: spam -- https://main.php.net/note/delete/127940/spam
Del: non-english -- https://main.php.net/note/delete/127940/non-english
Del: in docs -- https://main.php.net/note/delete/127940/in+docs
Del: other reasons-- https://main.php.net/note/delete/127940
Reject -- https://main.php.net/note/reject/127940
Search -- https://main.php.net/manage/user-notes.php