2

I am trying to get the filename of the class which is the child of the top of the hierarchy, where the depth of the class hierarchy is not known.

I.e. I'm trying to work out what code to put inside baseApplicationClass in order to get the filename of myApplicationClass, which will work in both examples:

baseApplicationClass L abstract myApplicationClass extends baseApplicationClass L myInstanceClass extends myApplicationClass 

and

baseApplicationClass L myApplicationClass extends baseApplicationClass 

Using get_class ($this) doesn't work, because that gives an inconsistent result between these two examples.

In both cases, I need to get the filename of myApplicationClass.

Note that the baseApplicationClass obviously has a known name, whereas the other one/two classes do not.

With a two-level hierarchy (i.e. second example), I can obviously do:

$reflector = new ReflectionClass (get_class ($this)); $applicationRoot = dirname ($reflector->getFileName ()); 

but this fails for a three-level hierarchy.

1 Answer 1

1

I found a solution:

# Get the classes in order, starting with baseApplicationClass, but not including the last in the chain $classHierarchy = array_reverse (array_values (class_parents ($this))); # Add in the last in the chain $classHierarchy[] = get_class ($this); # Get myApplicationClass $myApplicationClassName = $classHierarchy[1]; // Second in chain i.e. the direct child of baseApplicationClass # Reflect as normal $reflector = new ReflectionClass ($myApplicationClassName); $this->applicationRoot = dirname ($reflector->getFileName ()); 

Though perhaps there is something cleaner!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.