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
}
}
Not really sure what purpose it serves, but here you go:
class A{
final protected function __construct(){
$obj = new ReflectionClass($this);
$filename = $obj->getFileName();
}
}
You can cheat and use debug_backtrace:
class A {
final protected function __construct() {
$stacktrace = @debug_backtrace(false);
$filename = $stacktrace[0]['file'];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With