简体   繁体   中英

PHP get the file name from which the present function is called

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.

Please help me out, thanks.

Edit: I have a function in test.php which can be called from ajax, something like

function getSomething($params){
    check($params);

    check($params);
}

It executes one function check two times.

I have another function called check() in common.php, something like

function check($params){
  //Here, i want know where the file name and line at which it has been called.
}

Edit: Need to get the lines of at which the function has been triggered to check in check function.

It has been executed two times in test.php

First execution-> We need to get test.php, line of first function call

Second execution-> We need to get test.php, line of second function

You need to obtain a backtrace and extract the information from the resulting array. Beware that before PHP/5.4 you were not able to limit how deep the trace goes thus output can be fairly large. It's alright to use it for debugging purposes but I wouldn't abuse it for anything else.

the constant __FILE__ contains the filename and full path (see http://www.php.net/manual/en/language.constants.predefined.php ). This changes to the included library etc.

What you may want is:

$_SERVER["PHP_SELF"] which reflects the currently running script.

You can take alook at debug_backtrace() http://nl3.php.net/debug_backtrace . This function returns an array with all the files

如果要避免可能昂贵的debug_backtrace,则需要在函数签名中添加at参数并传递常量FILE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM