Bug #78954 [Nab]: get_object_vars not returning typed empty properties
| From: | thomas dot wiringa at gmail dot com | Date: | Thu, 12 Dec 2019 13:59:36 +0000 |
| Subject: | Bug #78954 [Nab]: get_object_vars not returning typed empty properties | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-224249@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78954&edit=1
ID: 78954
User updated by: thomas dot wiringa at gmail dot com
Reported by: thomas dot wiringa at gmail dot com
Summary: get_object_vars not returning typed empty properties
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Arch Linux / Linux 5.4.2
PHP Version: 7.4.0
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2019-12-12 13:52:42] nikic@php.net
get_object_vars() does not return null, because those values are not null. These properties have not
been initialized -- essentially they do not exist.
If you wish to get all the properties that were *declared* in the class, regardless of whether they
have been initialized or unset, you can use ReflectionObject.
------------------------------------------------------------------------
[2019-12-12 13:33:21] thomas dot wiringa at gmail dot com
Description:
------------
When using typed properties such as
public string $foo; or public ?string
$foo; get_object_vars does not return [ 'foo' => NULL ] if this
property was never set. However, pre-7.4 it would return the property in the array if it was defined
as public $foo; but never set/changed.
Test script:
---------------
https://3v4l.org/eg6o0
Expected result:
----------------
string(20) "bar exists in $vars"
string(28) "foobar exists in $vars"
string(20) "foo exists in $vars"
Actual result:
--------------
string(19) "bar exists in $vars"
Warning: assert(): assert(array_key_exists('foobar', $vars)) failed in /in/eg6o0 on line
12
string(30) "foobar does not exist in $vars"
Warning: assert(): assert(array_key_exists('foo', $vars)) failed in /in/eg6o0 on line 13
string(27) "foo does not exist in $vars"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78954&edit=1