2

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
    }
}
Shiboe
  • 787
  • 6
  • 8
  • 1
    May I know why you can't use debug_backtrace()? – Vijay Oct 05 '12 at 05:17
  • i suppose you meant `$this->path_to_caller = dirname( $file );` in the first block of code. – air4x Oct 05 '12 at 05:23
  • Vijay - Don't want to use backtrace() since it would be an inefficient parsing solution, considering I can still simply pass the directory into the constructor. Ideally there is a built in function or something that I just am not able to find. air4x - Yeah, I did, thanks. :) – Shiboe Oct 05 '12 at 05:32
  • May we know why you think you need this? Because chances are you are doing something wrong. – Gordon Oct 05 '12 at 06:01
  • I was building an addon class that would be instantiated in an addon init file. The addon object gets pushed into a collection that would later be iterated to load appropriate addons according to the specified route. By passing the init file's location into the addon class, I can then load whatever additional addon files are sibling to it at that later load time, without having to statically define them for inclusion. I may however go with a simpler design for efficiency. – Shiboe Oct 05 '12 at 06:23

0 Answers0