php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53357 Problem with getMethod of ReflectionClass
Submitted: 2010-11-19 16:31 UTC Modified: 2010-11-21 13:05 UTC
From: jonas dot jones at gmx dot ch Assigned: johannes (profile)
Status: Not a bug Package: Reflection related
PHP Version: 5.3.3 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: jonas dot jones at gmx dot ch
New email:
PHP Version: OS:

 

 [2010-11-19 16:31 UTC] jonas dot jones at gmx dot ch
Description:
------------
---
From manual page: http://www.php.net/reflectionclass.getmethod
---

If you use getMethod() on a ReflecitonClass Object, for a class, that inherits its method from a parent class, you get a ReflectionMethod Object, that links to the parent class. There is no possibility to get a ReflectionMethod Object, that is connected to the class linked to the ReflectionClass Object.

This beheaviour is not nice, if you want to use the ReflectionMethod Object to call a static method. We could not figure out a possibility to call the method on the child class.

A second argument on getMethod() would be nice. It could be a flag, wether getMethod should return a ReflectionMethod object, which is connected to the class where the method was defined or an object which is connected to the class, that was used for the ReflectionClass.

Test script:
---------------
class ParentClass {
   public static function foo(){
      echo get_called_class();
   }
}
class ChildClass extends MainClass {
}

$class = new ReflectionClass('ChildClass');
$method = $class->getMethod('foo');
$method->invoke(null); // outputs ParentClass instead of expected ChildClass

Expected result:
----------------
ChildClass

Actual result:
--------------
ParentClass

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-19 16:34 UTC] pajoye@php.net
-Operating System: Windows 7/all +Operating System:
 [2010-11-20 01:37 UTC] johannes@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: johannes
 [2010-11-20 01:37 UTC] johannes@php.net
The reflection API is supposed to tell "the truth". Extending the ReflectionClass::getMethod() method is therefore no option. The only thing I can imagine is hanving ReflectionMethod::invoke() to accept a string, instead of an instance, as first parameter for defining the scope. I will look into that.
 [2010-11-21 13:05 UTC] johannes@php.net
-Status: Assigned +Status: Bogus
 [2010-11-21 13:05 UTC] johannes@php.net
See bug #38992 for the request of passing a class name as string.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Oct 23 15:01:31 2019 UTC