Doc #78954 [Com]: get_object_vars not returning typed empty properties

From: Date: Thu, 12 Dec 2019 14:09:02 +0000
Subject: Doc #78954 [Com]: get_object_vars not returning typed empty properties
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17107@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
 Comment by:         retertertert at fgfgfg dot com
 Reported by:        thomas dot wiringa at gmail dot com
 Summary:            get_object_vars not returning typed empty properties
 Status:             Re-Opened
 Type:               Documentation Problem
 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:

> 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


Previous Comments:
------------------------------------------------------------------------
[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

------------------------------------------------------------------------
[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


Thread (4 messages)

« previous php.doc.bugs (#17107) next »