php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69804 ReflectionClass::getStaticPropertyValue() throws exception on protected props
Submitted: 2015-06-11 22:19 UTC Modified: 2020-06-24 09:21 UTC
From: grzegorz129 at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Reflection related
PHP Version: 7.0Git-2015-06-11 (snap) OS: OSX, Linux
Private report: No CVE-ID: None
 [2015-06-11 22:19 UTC] grzegorz129 at gmail dot com
Description:
------------
Trying to use ReflectionClass::getStaticPropertyValue() on protected static properties of class results in exception with information about non existing property. However ReflectionClass::getStaticProperties() returns that property with correct value on all supported PHP versions.
Adding ReflectionProperty::setAccessible(true) doesn't affect final effect.

Of course I can use getStaticProperties() array, but maybe getStaticPropertyValue() should be fixed or at least some documentation note be added?

After research I found it's related to #54620, but maybe setAccessible() should also affect getStaticPropertyValue() call?

Test script:
---------------
<?php

class foo {
    protected static $test = 'bar';
}

$classReflection = new ReflectionClass('foo');
$property = $classReflection->getProperty('test');
var_dump($property);

$property->setAccessible(true); //Changes nothing in that case
var_dump($classReflection->getStaticProperties());
var_dump($classReflection->getStaticPropertyValue('test'));

Actual result:
--------------
http://3v4l.org/om1lZ

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-12 01:33 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2015-06-12 01:33 UTC] laruence@php.net
hmm, setAccess doesn't change the property_info...  will think about a proper hack fix..
 [2015-06-13 13:31 UTC] laruence@php.net
-Type: Bug +Type: Documentation Problem -Assigned To: laruence +Assigned To:
 [2015-06-13 13:31 UTC] laruence@php.net
-Status: Open +Status: Assigned
 [2015-06-13 22:09 UTC] grzegorz129 at gmail dot com
@laruence: I saw your another comment about method behaving similar in PHP 5.6 - you're right, but I think implementation should be fixed instead of documentation, because that behaviour is little strange.
It shouldn't break any code - who's calling setAccess(true) and expect it wouldn't change that? :D
 [2015-06-13 23:13 UTC] bwoebi@php.net
-Status: Open +Status: Assigned -Type: Documentation Problem +Type: Bug -Assigned To: +Assigned To: laruence
 [2015-06-13 23:13 UTC] bwoebi@php.net
@laruence: IMO this is a bug.

It should just allow getStaticPropertyValue() to actually work on protected variables too. Or remove protected variables from getStaticProperties().

It definitely is a major inconsistency which should be considered as bug.

@grzegorz129: But don't expect ReflectionProperty::setAccessible() could change anything here.
setAccessible() only affects the scope of that particular ReflectionProperty object.
 [2020-04-15 16:36 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #69804: ::getStaticPropertyValue() throws on protected props
On GitHub:  https://github.com/php/php-src/pull/5367
Patch:      https://github.com/php/php-src/pull/5367.patch
 [2020-06-24 09:20 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=26aefb750a614391cdb8ee3f63d66f46ed57bd55
Log: Fix #69804: ::getStaticPropertyValue() throws on protected props
 [2020-06-24 09:20 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2020-06-24 09:21 UTC] cmb@php.net
-Assigned To: laruence +Assigned To: cmb
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 10:01:30 2024 UTC