# Let ReflectionMethod keep track of original class

- Thread: https://externals.io/message/120448
- Original archive: https://news-web.php.net/php.internals/120448
- Started by Andreas Hennings on 2023-05-30T02:42:55+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #120448 — Andreas Hennings — 2023-05-30T02:42:55+00:00

URL: https://externals.io/message/120448#120448

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

---

## #120468 — Larry Garfield — 2023-05-30T16:47:31+00:00

URL: https://externals.io/message/120448#120468 · In reply to: #120448

-- 
  Larry Garfield
  larry@garfieldtech.com


> 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.


I would not be opposed to this.  I don't know that I have any use cases for it, but I'd be open to it.

--Larry Garfield

---

## #120469 — Andreas Hennings — 2023-05-30T16:54:30+00:00

URL: https://externals.io/message/120448#120469 · In reply to: #120468

>
>
>
> --
>   Larry Garfield
>   larry@garfieldtech.com
>
> 
> > […]
>
>
> I would not be opposed to this.  I don't know that I have any use cases for it, but I'd be open to it.

You can search in your favourite project's /vendor/ directory for
methods with two or more parameters that receive reflector objects.

"function .*\(.*Reflection\w+ \$\w+, .?Reflection\w+ \$\w+"

>
> --Larry Garfield
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
