Alex
Alex

Reputation: 68460

get filename of extended class

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):

class B extends A{

}

File 1:

class A{

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

}

Upvotes: 9

Views: 3242

Answers (2)

Ben Lee
Ben Lee

Reputation: 53319

You can cheat and use debug_backtrace:

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

Upvotes: 4

user142162
user142162

Reputation:

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

class A{

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

}

Upvotes: 19

Related Questions