php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64592 ReflectionClass::getMethods() returns methods out of scope
Submitted: 2013-04-05 16:14 UTC Modified: 2016-06-29 15:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: benjamin dot morel at gmail dot com Assigned:
Status: Closed Package: Reflection related
PHP Version: 5.4.13 OS: Linux
Private report: No CVE-ID: None
 [2013-04-05 16:14 UTC] benjamin dot morel at gmail dot com
Description:
------------
As far as I understand it, ReflectionClass::getMethods() should return only the 
methods that are in the scope of the reflected class, thus excluding private 
methods from parent classes.

That's moreover the behaviour exposed by ReflectionClass::getProperties(), making 
the two methods behave in different manners.

Test script:
---------------
class Foo {
    private $a;
    private function a() {}

    protected $b;
    protected function b() {}
}
class Bar extends Foo {
    private $c;
    private function c() {}
}

$r = new ReflectionClass('Bar');

echo 'Properties in scope: ';
foreach ($r->getProperties() as $property) {
    echo $property->getName() . ' ';
}

echo PHP_EOL, 'Methods in scope: ';
foreach ($r->getMethods() as $method) {
    echo $method->getName() . ' ';
}

Expected result:
----------------
Properties in scope: c b 
Methods in scope: c b 

Actual result:
--------------
Properties in scope: c b 
Methods in scope: c a b 

Patches

bug64592.patch (last revision 2013-04-06 13:19 UTC by laruence@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-06 02:08 UTC] laruence@php.net
for php, even the private inherited function is not "visible", but it exists in 
the child function table.
 [2013-04-06 11:54 UTC] benjamin dot morel at gmail dot com
But at least, getMethods() and getProperties() should behave in the same way, 
shouldn't they?
 [2013-04-06 13:19 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug64592.patch
Revision:   1365254381
URL:        https://bugs.php.net/patch-display.php?bug=64592&patch=bug64592.patch&revision=1365254381
 [2013-04-06 15:11 UTC] benjamin dot morel at gmail dot com
Works like a charm with your patch, thanks!
Any chance that gets into 5.4, or at least 5.5 (if there is a fear of breaking BC 
with existing libraries that would rely on this behaviour)?
 [2013-04-06 15:27 UTC] felipe@php.net
-Status: Open +Status: Not a bug
 [2013-04-06 15:27 UTC] felipe@php.net
It is not intended to just show the accessible ones, hence we already have introduced method like ReflectionMethod::setAccessible().
 [2013-04-06 16:25 UTC] benjamin dot morel at gmail dot com
@felipe, did you read the bug before closing it? We're not talking about "not 
accessible", but "not in scope".
This is totally different.

The fact is, if you run my example, getMethods() and getProperties() do not behave 
in the same way, thus either this is a bug in getMethods(), and if not, this is a 
bug in getProperties().

But I'm pretty sure it's getProperties() that behaves correctly here.
Could you please comment on this?
 [2013-04-06 16:46 UTC] felipe@php.net
-Status: Not a bug +Status: Open
 [2013-04-07 12:53 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: johannes
 [2013-04-07 12:53 UTC] felipe@php.net
Hey Johannes, what do you think about this behavior? Since reflection has worked in this way for a long time...
 [2013-04-08 08:04 UTC] johannes@php.net
-Status: Assigned +Status: Open
 [2013-04-08 08:04 UTC] johannes@php.net
In general reflection in PHP leaks the "truth" by not hiding implementation details. This falls into this category. Telling the truth is nice for maintainers but not really for users.

We can't change it in released (or feature frozen) versions so 5.6 might be an option. For that we might collect more such cases and think about ringing Reflection on some higher level of abstraction.
 [2013-04-08 08:08 UTC] johannes@php.net
-Status: Assigned +Status: Open -Assigned To: johannes +Assigned To:
 [2013-04-08 08:54 UTC] benjamin dot morel at gmail dot com
@johannes too bad this comes up just a few days after the feature freeze for 5.5 :(
No chance this could be considered a bug, not a feature, and make it into 5.5?

I personally don't think it would be a huge problem, as most existing libraries 
probably haven't started to modify their code for 5.5 compatibility, so it could 
still be time to fix it?
 [2016-06-29 15:13 UTC] cmb@php.net
I think that suppressing inherited private properties, but showing
inherited private methods is very inconsistent behavior. However,
changing one or the other would likely require the RFC process,
see <https://wiki.php.net/rfc/howto>.
 [2016-06-29 15:17 UTC] benjamin dot morel at gmail dot com
7.0 would have been a perfect time for this. Too bad nobody took this bug seriously at the time.
 [2020-02-28 14:54 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=87bc99439d5cdd3465ae37f1207067fc40ab7b19
Log: Fixed bug #64592
 [2020-02-28 14:54 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC