PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Find a root class of a class

Open giacomomasseron opened this issue 4 months ago • 1 comments

Hi,
I need to find the root class of a class.
I tried to use AI, but the code does not work.

class BaseClass {}

class Parent extends BaseClass {}

class Child extends Parent {}

I would need something like:

$root = findRoot('Child'); // It should return "BaseClass"

giacomomasseron avatar Aug 07 '25 06:08 giacomomasseron

You can either use php-src builtin reflection https://www.php.net/manual/en/reflectionclass.getparentclass.php

or https://github.com/Roave/BetterReflection in case you do not want to load/execute code

staabm avatar Aug 07 '25 06:08 staabm