Doc #78954 [ReO->Csd]: get_object_vars not returning typed empty properties
Edit report at https://bugs.php.net/bug.php?id=78954&edit=1
ID: 78954
Updated by: cmb@php.net
Reported by: thomas dot wiringa at gmail dot com
Summary: get_object_vars not returning typed empty properties
-Status: Re-Opened
+Status: Closed
Type: Documentation Problem
Package: Scripting Engine problem
Operating System: Arch Linux / Linux 5.4.2
PHP Version: 7.4.0
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
> public $foo; is the same as public $foo = null; but it is not
> the same as doing public ?string $foo;
Yes.
Previous Comments:
------------------------------------------------------------------------
[2019-12-12 17:18:48] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=348498
Log: Fix #78954: get_object_vars not returning typed empty properties
Untyped properties default to NULL, so this sentence is superfluous for those.
Unitialized typed properties are not included in the result, so the sentence is
plain wrong for those.
------------------------------------------------------------------------
[2019-12-12 14:14:41] thomas dot wiringa at gmail dot com
I see! Thanks for clarifying, I didn't know that typed properties would behave differently like
that.
So for future reference:
public $foo; is the same as public $foo = null; but it is not the same as doing public ?string $foo;
------------------------------------------------------------------------
[2019-12-12 14:09:02] retertertert at fgfgfg dot com
> in previous PHP versions, those properties would still
> be visible in the array returned by get_object_vars
don't match
> not returning typed empty properties
becaus ein previous versions they couldn't be typed
------------------------------------------------------------------------
[2019-12-12 14:07:55] nikic@php.net
> I understand that those properties have never been initialized, however, in previous PHP
> versions, those properties would still be visible in the array returned by get_object_vars. Is this
> an undocumented change then?
No. The behavior is exactly the same as in previous PHP versions: Properties that have a value are
included, properties that are unset are not. The only difference is that typed properties start out
in an uninitialized state, while untyped properties start with a null default value. If you wish the
default value to be null, please add an explicit "= null" initializer. (Though of course I
would not recommend doing so without good reason.)
> Because the documentation for get_object_vars still suggests that it should be returning NULL
> for unassigned properties: "If a property has not been assigned a value, it will be returned
> with a NULL value."
You are correct that the phrasing in the get_object_vars() documentation is wrong. The sentence
"If a property has not been assigned a value, it will be returned with a NULL value"
should be removed without replacement. I'm reclassifying this as a documentation issue.
------------------------------------------------------------------------
[2019-12-12 13:59:36] thomas dot wiringa at gmail dot com
I understand that those properties have never been initialized, however, in previous PHP versions,
those properties would still be visible in the array returned by get_object_vars. Is this an
undocumented change then? Because the documentation for get_object_vars still suggests that it
should be returning NULL for unassigned properties: "If a property has not been assigned a
value, it will be returned with a NULL value."
I also noticed that the behaviour seems inconsistent with get_class_vars now, as that function still
returns the expected array, with the difference being that get_class_vars returns the initial values
instead of the current values.
An example: https://3v4l.org/fFpe4
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=78954
--
Edit this bug report at https://bugs.php.net/bug.php?id=78954&edit=1
Thread (4 messages)