Php 获取类层次结构顶部的子级的文件名,无论继承级别如何

Php 获取类层次结构顶部的子级的文件名,无论继承级别如何,php,Php,我试图获取类的文件名,它是层次结构顶部的子级,而类层次结构的深度未知 也就是说,我正试图找出要放入baseApplicationClass中的代码,以获取myApplicationClass的文件名,这在两个示例中都适用: baseApplicationClass L abstract myApplicationClass extends baseApplicationClass L myInstanceClass extends myApplicationClass

我试图获取类的文件名,它是层次结构顶部的子级,而类层次结构的深度未知

也就是说,我正试图找出要放入
baseApplicationClass
中的代码,以获取
myApplicationClass
的文件名,这在两个示例中都适用:

baseApplicationClass
 L
   abstract myApplicationClass extends baseApplicationClass
    L
      myInstanceClass extends myApplicationClass

使用
get\u类($this)
不起作用,因为这会在这两个示例之间产生不一致的结果

在这两种情况下,我都需要获取myApplicationClass的文件名

请注意,baseApplicationClass显然有一个已知的名称,而其他一个或两个类则没有

对于两级层次结构(即第二个示例),我显然可以做到:

$reflector = new ReflectionClass (get_class ($this));
$applicationRoot = dirname ($reflector->getFileName ());
但这对于三级层次结构来说是失败的。

我找到了一个解决方案:

# 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 ());
虽然也许有更干净的东西

# 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 ());