获取子类的文件路径

时间:2011-10-10 10:44:49

标签: php oop

要获取子类的名称,我使用get_called_class()

我应该使用什么来获取子类的文件路径?

1 个答案:

答案 0 :(得分:6)

使用反射,特别是ReflectionClass::getFileName()。如何实例化ReflectionClass完全取决于当前范围,例如

// globally
$reflectionClass = new ReflectionClass('SubClassName');

// within the sub class
$reflectionClass = new ReflectionClass(__CLASS__);

// within either sub or parent class in a static method
$reflectionClass = new ReflectionClass(get_called_class());

// within either sub or parent class, provided the instance is a sub class
$reflectionClass = new RelfectionObject($this);

// filename
$fn = $reflectionClass->getFileName();

// what I assume you mean by "path"
$path = dirname($fn);