Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get filename of extended class

Tags:

php

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
  }

}
like image 558
Alex Avatar asked Dec 03 '11 00:12

Alex


2 Answers

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

class A{

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

}
like image 142
Tim Cooper Avatar answered Oct 28 '22 11:10

Tim Cooper


You can cheat and use debug_backtrace:

class A {
  final protected function __construct() {
    $stacktrace = @debug_backtrace(false);
    $filename = $stacktrace[0]['file'];
  }
}
like image 38
Ben Lee Avatar answered Oct 28 '22 09:10

Ben Lee