Forum Moderators: coopster

Message Too Old, No Replies

Get path of file calling function

         

Nutter

2:28 am on Apr 24, 2008 (gmt 0)

10+ Year Member



I'm trying to come up with a way for a function to know the path of the file that called it. Say I have function f() in file 123.php. I call f() from file abc.php. I want f() to be able to figure out that it was called from abc.php and the path to abc.php. I assume calling f(__FILE__) would work, but I'd prefer not to have to do that if I don't have to.

edit...
There's something about typing out a message here that makes the next Google search find what I'm looking for. debug_backtrace() does exactly what I'm trying to do.

[edited by: Nutter at 2:32 am (utc) on April 24, 2008]

jatar_k

3:20 am on Apr 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> something about typing out a message here

very often the case

my first thought would have been looking through $_SERVER vars

why wouldn't you want to use __FILE__ ?

Nutter

10:15 am on Apr 24, 2008 (gmt 0)

10+ Year Member



Using __FILE__ isn't the problem. It's just that if possible I didn't want to have to include it as part of the function. It's easier to type f() than f(__FILE__). And using __FILE__ within the function would give the file that the function is in, not the file that called it.

Nutter

1:44 pm on Apr 24, 2008 (gmt 0)

10+ Year Member



That last message was really vague...

What I'm doing is coming up with a function that will tell a plugin the path to itself. That way the plugin can call pluginPath() and know where it is. Yes, I know the plugin author can use __FILE__ to get the path, but it seems easier to have a couple of functions that the plugin author can use rather than having them rely on __FILE__. That way I can do the messy part and leave the plugin with a nice clean function to use.

coopster

2:38 pm on Apr 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



In PHP5 classes you can also throw an Exception [php.net]