包含文件内__FILE__的PHP输出 [英] PHP output of __FILE__ inside included file
问题描述
好的,这是一个真正的简短查询。
我正在从函数内部调用 __ FILE __
。
现在,此函数本身位于必需的文件中。
Ok, here is a real short query.
I am calling __FILE__
from inside a function.
Now, this function itself is in a required file.
现在,当我从父文件内部调用此函数时, __ FILE __
输出父文件还是包含的文件?
Now, when I call this function from inside the Parent file, will the __FILE__
output the parent file or the file which was included?
哦,我正在寻找可以确认的来源,如果可能,因为这里的测试完全给了我荒谬的结果。
Oh, and I am looking for a source where I can confirm, if possible, because my tests here are giving me entirely absurd results.
此外,如果这应该显示子文件(包含的),我应该如何处理它而是显示父文件路径? (是否有变化?)
Also, if this should display the child (included) file, how should I go about it so that it rather displays the parent filepath? (some variation or something?)
推荐答案
__ FILE __
始终替换为
要获取调用函数的文件的名称,可以使用 debug_backtrace()
。这会以数组的形式返回当前的调用堆栈,每个子数组都包含进行调用的文件,行和功能键。
To get the name of the file from which a function was called, you can use debug_backtrace()
. This returns the current callstack as an array, with each sub-array containing the file, line and function keys from which the call was made.
您可以将前面的数组中的大多数元素可获取调用函数的位置:
You can shift the front-most element off the array to get the location from which a function was called:
<?php
require_once('b.php');
b();
b.php:
b.php:
<?php
function b() {
$bt = debug_backtrace();
var_export($bt);
}
输出:
output:
array (
0 => array (
'file' => '/home/meagar/a.php',
'line' => 5,
'function' => 'b',
'args' => array( ),
),
)
同样的事情无需函数调用即可工作:
The same thing works without function calls:
<?php require_once('b.php');
b.php:
b.php:
<?php
$bt = debug_backtrace();
var_export($bt);
输出:
output:
array (
0 => array (
'file' => '/home/meagar/a.php',
'line' => 3,
'function' => 'require_once',
),
)
这篇关于包含文件内__FILE__的PHP输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!