php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79236 Uninitialized properties are hidden from get_object_vars
Submitted: 2020-02-06 16:09 UTC Modified: 2020-02-06 19:00 UTC
From: raincomplain at outlook dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.4.2 OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: raincomplain at outlook dot com
New email:
PHP Version: OS:

 

 [2020-02-06 16:09 UTC] raincomplain at outlook dot com
Description:
------------
Little bit of hesitation on this one but I had to report it since no answers were found on SO 

https://stackoverflow.com/q/60087384/5999372

So uninitialized properties are hidden from get_object_vars but exposed to get_class_vars like as Null

Test script:
---------------
class A {
    public int $num1;
    public $num2;
}

var_dump(get_object_vars(new A));
var_dump(get_class_vars('A'));

Expected result:
----------------
array(1) {
  ["num1"]=>
  NULL
  ["num2"]=>
  NULL
}
array(2) {
  ["num1"]=>
  NULL
  ["num2"]=>
  NULL
}

Here to be honest I'm not sure whether get_class_vars should not show num1 and treat it as Null or whether get_object_class should show num1 either, but I assume they should output the same result especially since the properties are public.

Actual result:
--------------
array(1) {
  ["num2"]=>
  NULL
}
array(2) {
  ["num1"]=>
  NULL
  ["num2"]=>
  NULL
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-06 19:00 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-02-06 19:00 UTC] requinix@php.net
This is working as intended.

get_object_vars() does not show the property because it's uninitialized: it's not appropriate to list it as num1 => null because the value is not null - which isn't even a valid value for it. It's omitted unless/until there is a value assigned.

get_class_vars() shows the property because it is defined on the class. There is a distinct utility to showing that the property is there, even if its default value is uninitialized.

This all is very much like how unset()ing properties works. https://3v4l.org/lAIEX
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC