Question

Let's say we have index.php and it is stored in /home/user/public/www and index.php calls the class Foo->bar() from the file inc/app/Foo.class.php.

I'd like the bar function in the Foo class to get a hold of the path /home/user/public/www in this instance — I don't want to use a global variable, pass a variable, etc.

Was it helpful?

Solution

Wouldn't this get you the directory of the running script more easily?

$dir=dirname($_SERVER["SCRIPT_FILENAME"])

OTHER TIPS

You can use debug_backtrace to look at the calling path and get the file calling this function.

A short example:

class Foo {
  function bar() {    
   $trace = debug_backtrace();
   echo "calling file was ".$trace[0]['file']."\n";
  }
}

getcwd() gets the current working directory

It can be changed for a variety of reasons by 3rd party modules, includes or even your own code by issuing a chdir().

debug_backtrace() as Devon suggested is the answer you're looking for.

Found it. getcwd().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top