获取扩展类的文件名

时间:2011-12-03 00:04:59

标签: php class

假设我有两个文件,每个文件都有一个类。如何在父类中获取子类的文件名?

文件2(子类):

class B extends A{

}

文件1:

class A{

  final protected function __construct(){
    // here I want to get the filename where class B is, 
    // or whatever class is the child
  }

}

2 个答案:

答案 0 :(得分:16)

不确定它的目的是什么,但是你走了:

class A{

  final protected function __construct(){
    $obj = new ReflectionClass($this);
    $filename = $obj->getFileName();
  }

}

答案 1 :(得分:3)

您可以欺骗并使用debug_backtrace

class A {
  final protected function __construct() {
    $stacktrace = @debug_backtrace(false);
    $filename = $stacktrace[0]['file'];
  }
}