get filename of extended class

前端 未结 2 1738
心在旅途
心在旅途 2020-12-07 01:25

Let\'s say I have two files, each one has a class. How can I get the filename where the child class is, within the parent class?

File 2 (child class):



        
相关标签:
2条回答
  • 2020-12-07 01:54

    You can cheat and use debug_backtrace:

    class A {
      final protected function __construct() {
        $stacktrace = @debug_backtrace(false);
        $filename = $stacktrace[0]['file'];
      }
    }
    
    0 讨论(0)
  • 2020-12-07 02:07

    Not really sure what purpose it serves, but here you go:

    class A{
    
      final protected function __construct(){
        $obj = new ReflectionClass($this);
        $filename = $obj->getFileName();
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题