php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78319 get_class_vars/ReflectionClass::getDefaultProperties don't show typed propertie
Submitted: 2019-07-22 07:53 UTC Modified: 2019-07-24 08:53 UTC
From: nicolas dot grekas+php at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: 7.4.0alpha3 OS:
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: nicolas dot grekas+php at gmail dot com
New email:
PHP Version: OS:

 

 [2019-07-22 07:53 UTC] nicolas dot grekas+php at gmail dot com
Description:
------------
get_class_vars()/ReflectionClass::getDefaultProperties() don't show typed properties.

I get they aren't yet initialized, but we could report them as "null" to no break scripts that look only for their names.

Test script:
---------------
class Php74
{
    public $p1 = 123;
    public \stdClass $p2;

    public function __construct()
    {
        $this->p2 = new \stdClass();
    }
}

print_r(get_class_vars('Php74'));

$r = new \ReflectionClass('Php74');

print_r($r->getDefaultProperties());

print_r($r->getProperties());

Expected result:
----------------
Array
(
    [p1] => 123
    [p2] => 
)
Array
(
    [p1] => 123
    [p2] => 
)
Array
(
    [0] => ReflectionProperty Object
        (
            [name] => p1
            [class] => Php74
        )

    [1] => ReflectionProperty Object
        (
            [name] => p2
            [class] => Php74
        )

)

Actual result:
--------------
Array
(
    [p1] => 123
)
Array
(
    [p1] => 123
)
Array
(
    [0] => ReflectionProperty Object
        (
            [name] => p1
            [class] => Php74
        )

    [1] => ReflectionProperty Object
        (
            [name] => p2
            [class] => Php74
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-22 15:07 UTC] nikic@php.net
Not sure about this one. I can see how not returning them can be a problem, but just returning null also seems wrong, because it's not the actual default value.
 [2019-07-22 15:24 UTC] nicolas dot grekas+php at gmail dot com
In any case, existing scripts will need to be adapted. The cases that were affected in my situation were only looking at keys, without caring about the default values.
Not returning typed properties is equally wrong to be, so the best approximation is "null" to me, because it allows not breaking scripts like mines. Others that look for values will need to be adapted in whatever the behavior anyway.
 [2019-07-23 08:27 UTC] nicolas dot grekas+php at gmail dot com
Another reason why this is important: get_class_vars() takes the scope into account so that now it's much harder to get props for the current scope.
 [2019-07-23 10:47 UTC] nikic@php.net
I've opened https://github.com/php/php-src/pull/4463 for get_class_vars(). I agree that having them with a null value is better than not having them.

I don't think we should include them in getDefaultProperties() though. As far as I know that's the only reflection API that actually provides default values -- if we change it, I don't think there will even be a way to distinguish a null default / an uninitialized default from reflection. As getProperties() does include them and getDefaultProperties() is specifically about defaults (which don't exist here), I think we should leave that one alone.
 [2019-07-24 08:53 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-07-24 08:53 UTC] nikic@php.net
The get_class_vars() part is fixed in https://github.com/php/php-src/commit/a49d53baa2ad2fb6a951360f0083a883e7631370, the getDefaultProperties() part is "won't fix", so closing here.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC