php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #78954 get_object_vars not returning typed empty properties
Submitted: 2019-12-12 13:33 UTC Modified: 2019-12-12 17:20 UTC
From: thomas dot wiringa at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.4.0 OS: Arch Linux / Linux 5.4.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: thomas dot wiringa at gmail dot com
New email:
PHP Version: OS:

 

 [2019-12-12 13:33 UTC] 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"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-12 13:52 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2019-12-12 13:52 UTC] 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:59 UTC] 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 14:07 UTC] nikic@php.net
-Status: Not a bug +Status: Re-Opened -Type: Bug +Type: Documentation Problem
 [2019-12-12 14:07 UTC] 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 14:09 UTC] 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:14 UTC] 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 17:18 UTC] 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 17:20 UTC] cmb@php.net
-Status: Re-Opened +Status: Closed -Assigned To: +Assigned To: cmb
 [2019-12-12 17:20 UTC] cmb@php.net
> public $foo; is the same as public $foo = null; but it is not
> the same as doing public ?string $foo;

Yes.
 [2019-12-12 17:20 UTC] salathe@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=5aa25e795215a43633e4c56fdd518f37493ca30f
Log: Fix #78954: get_object_vars not returning typed empty properties
 [2019-12-13 07:18 UTC] mumumu@php.net
Automatic comment from SVN on behalf of mumumu
Revision: http://svn.php.net/viewvc/?view=revision&revision=348503
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.
 [2020-02-07 06:04 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=00ff0a3a0c1c3ffd26f9f9ab957bfc7fc8e3b1db
Log: Fix #78954: get_object_vars not returning typed empty properties
 [2020-12-30 11:59 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=c5f1759cba46d97f5b850c048ff2dc7d9aaddb38
Log: Fix #78954: get_object_vars not returning typed empty properties
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC