php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77772 ReflectionClass::getMethods(null) doesn't work
Submitted: 2019-03-20 14:19 UTC Modified: 2019-03-20 14:47 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: chokolatrix at gmail dot com Assigned:
Status: Closed Package: Reflection related
PHP Version: 7.2.16 OS:
Private report: No CVE-ID: None
 [2019-03-20 14:19 UTC] chokolatrix at gmail dot com
Description:
------------
---
From manual page: https://php.net/reflectionclass.getmethods
---

The function doesn't work as one would expect. 
Passing NULL (no filter) as parameter returns an empty array instead of all the methods.


Test script:
---------------
function getClassMethods($FQCN, $methodFilter = null): array
{
        
     foreach ((new \ReflectionClass($FQCN))->getMethods($methodFilter) as $methodObj) {
            $methodsNames[] = $methodObj->name;
     }


     return $methodsNames ?? [];
}


var_dump(getClassMethods(\Iterator::class));

Expected result:
----------------
array:5 [
  0 => "current"
  1 => "next"
  2 => "key"
  3 => "valid"
  4 => "rewind"
]


Actual result:
--------------
array(0) {
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-20 14:44 UTC] danack@php.net
Although the behaviour is not correct, passing null in place of an optional array parameter is not going to give you the correct result.

The function should be giving an error when passing null as the behaviour of optional parameters in internal functions should match that of userland functions:


ReflectionClass::getMethods(array $filter = []) {...}
 [2019-03-20 14:47 UTC] nikic@php.net
@danack: The optional parameter is not an array, but an integer. Currently null is interpreted as 0, which means everything is filtered out. I agree with the reporter that null should behave the same as not passing anything (i.e. no filter). I think we can safely make this change, as the current behavior is not useful in any way, so I'm not particularly concerned about BC here.
 [2019-03-22 11:41 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=da35fa2cb8d454c8e797067e28e647030a5fe5df
Log: Fixed bug #77772
 [2019-03-22 11:41 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 17:01:29 2024 UTC