获取扩展类的文件名

Ale*_*lex 9 php class

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

文件2(子类):

class B extends A{

}
Run Code Online (Sandbox Code Playgroud)

档案1:

class A{

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

}
Run Code Online (Sandbox Code Playgroud)

Tim*_*per 16

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

class A{

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

}
Run Code Online (Sandbox Code Playgroud)

  • @webbiedave很难说OP是如何使用受保护的构造函数创建对象的,但它们可能有其原因.也许他们在'A`上有一个公共的,静态的工厂方法 (2认同)