包含文件内 __FILE__ 的 PHP 输出
好的,这是一个真正的简短查询。 我正在从函数内部调用 __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.
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
__FILE__
始终替换为出现该符号的文件名。要获取调用函数的文件的名称,您可以使用
debug_backtrace()
。这将当前调用堆栈作为数组返回,每个子数组包含进行调用的文件、行和函数键。您可以将最前面的元素从数组中移出,以获取调用函数的位置:
a.php:
b.php:
output:
同样的事情在没有函数调用的情况下也可以工作:
a.php:
b.php:
output:
__FILE__
is always replaced with the filename in which the symbol appears.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:
a.php:
b.php:
output:
The same thing works without function calls:
a.php:
b.php:
output:
文档说:
The document says:
这是我如何在我的一个项目中解决此问题的示例。
问题。
我在 html 模板的
head
部分中调用了以下方法:解决方案。
我将
__FILE__
常量作为参数传递:在页面的
head
中,我简单地这样调用:WhateverClass::loadPageSpecificStyle(__FILE__);
希望这有帮助!
Here's an example of how I fixed this in one of my projects.
The problem.
I had the following method which I was calling in the
head
section of my html template:The solution.
I passed the
__FILE__
constant as an argument:In the
head
of my page I then simply called like this:WhateverClass::loadPageSpecificStyle(__FILE__);
Hope this helps!
据我所知,您正在寻找的东西 - 获取“父级”包含的路径,其本身又是一个包含 - 是不可能的,甚至使用
debug_backtrace()
也是不可能的。您必须创建自己的包含函数来跟踪包含文件的“堆栈”。
To my knowledge, what you are looking for - getting the path of a "parent" include that in itself again is an include - is not possible, not even using
debug_backtrace()
.You would have to create your own include function that keeps track of a "stack" of included files.