Is there an easy way to get the file where a class

2019-06-03 13:23发布

问题:

I want to replicate this:

$myObject = new object( __FILE__ );

class object {

    protected $path_to_caller;

    public function __construct( $file ){
        $this->path_to_caller = dirname( $file );
    }
}

Without having to pass the parameter, since it will be the same for every call. Is there a way to access the calling file/directory within a member function without using debug_backtrace()?

Essentially, I want:

$myObject = new object();

class object {

    protected $path_to_caller;

    public function __construct(){
        $this->path_to_caller = special_function(); // dirname( __FILE__ ) of caller
    }
}
标签: php class