php - 我可以获取最初在包含文件中调用的 PHP 文件的路径吗?

假设我们有 index.php,它存储在 /home/user/public/www 中,index.php 调用inc/app/Foo.class.php 文件中的 Foo->bar() 类。

在这种情况下,我希望 Foo 类中的 bar 函数能够获取路径 /home/user/public/www — 我不知道不想使用全局变量,传递变量等。

最佳答案

您可以使用 debug_backtrace查看调用路径并获取调用此函数的文件。

一个简短的例子:

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

https://stackoverflow.com/questions/139794/

相关文章:

arrays - 在 C 中,是否可以创建没有 '\0(null)' 的字符串?

php - 如何在php中显示x天前的时间

haskell - 在 'let' 表达式中使用 'if'

c++ - assert 语句在 C++ 中不起作用

.net - 如何在 .NET 中替换“

python - 有没有一种很好的方法来拆分(可能)长的字符串而不用 Python 中的单词拆分?

perl - 如何在 Perl 中将本地时间转换为 Unix 时间戳?

email - 为什么 CR 和 LF 在电子邮件中一起出现如此重要?

flutter - 为什么 Visual Studio Code 中的代码文本没有颜色?

python - python中的快速过滤方法