Let ReflectionMethod keep track of original class
| From: | Andreas Hennings | Date: | Tue, 30 May 2023 02:42:55 +0000 |
| Subject: | Let ReflectionMethod keep track of original class | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-120448@lists.php.net to get a copy of this message | ||
Hello list,
this proposal will be useful in combination with "Declaration-aware attributes"
Problem
========
Currently, ReflectionMethod is not aware of the original class, if the
method is declared in a parent class.
Methods that are called during a discovery algorithm that need to
process a method with its original class typically need two
parameters:
function processMethod(\ReflectionClass $class, \ReflectionMethod $method) {..}
Proposal
========
Let a ReflectionMethod object keep track of the original class.
Introduce a new method ReflectionMethod->getOriginalClass() to retrieve it.
class B {
function f($x) {}
}
class C extends B {}
foreach ([
// There are different ways to get a reflection method object, all
of them track the original class.
new ReflectionMethod('C', 'f'),
(new ReflectionClass('C'))->getMethod('f'),
(new ReflectionMethod('C',
'f'))->getParameters()[0]->getDeclaringFunction(),
] as $rm) {
// The following won't change:
assert($rm->class === 'B');
assert($rm->getDeclaringClass()->getName() === 'B');
// New method:
assert($rm->getOriginalClass()->getName() === 'C');
Alternatives
==========
At first I thought we might introduce a new class like
"VirtualReflectionMethod" which behaves as if the method was declared
on the child class. But this is awkward.
I think the ->getOriginalClass() is much simpler.
--- Andreas