3

It's functionality is so strong that I worry about its stability and performance.

What do you think?

UPDATE

What I'm doing is this:

    $old_dir = getcwd();
    chdir( dirname($included_file) );
    include ( $included_file );
    chdir( $old_dir );

Essentially it just does include ( $included_file );,but inside that $included_file it can't find 3.php which is in the same directory as itself is in,so I manually set the cwd and it works.But it would be nice if I find the reason why it can't find.As for why debug_backtrace is needed,it's because 3.php is included by another func,since the relative path doesn't work,it has to use debug_backtrace to get the including file path,finally using the absolute path as mentioned below.

It's not easy to reproduce,as the above code is in the context of a method,and much more..If no one else has met this kinda problem I'd like to just stop here,anyway,the cost is just the 3 extra lines,not a big deal.

4

3 回答 3

6

debug_backtrace根据我的经验,它相对昂贵,因此您应该小心它不要在循环中使用(例如,在捕获警告或通知并每次执行回溯的自定义错误处理程序中)。

对于任何类型的错误记录,我认为它是非常宝贵的,因为它只会被调用一次,绝对不是性能问题。在错误报告中包含回溯肯定总是好的。

我不明白为什么这个函数的稳定性会有任何具体问题(即调用它会导致另一次崩溃),我从未听说过任何问题。当使用对象作为没有定义方法的函数参数时,我能看到的唯一“问题”是用户贡献的注释中的这个注释_toString

当然,您永远不应该将回溯的结果输出给最终用户——这是不言而喻的。

于 2010-03-13T13:10:18.613 回答
1

好吧,考虑到它的名字,我不确定我是否会将它用作我的应用程序的“正常”部分——即使我不记得读过任何说它好坏的东西。


我真的不知道您所说的“严重使用”是什么意思,但是:

  • 如果您的应用程序需要该功能才能工作,这可能表明您的设计存在问题
  • 当您想记录错误发生的方式/位置时,此功能在错误处理程序中很有用:在跟踪错误来源时,它将使日志文件更有用

不过,不确定“错误记录”是否符合您对严重使用的定义?

于 2010-03-13T12:53:32.093 回答
0

好的,据我了解,问题如下

你有一个 php 文件,我们称它为“main.php”。在“main.php”中,您将包含某个目录中的“A.php”:

# in "main.php"
include '/some/dir/A.php';

反过来,A.php 包含“B.php”,它与 A.php 位于同一目录中

# in "A.php"
include 'B.php'; 

B.php问题:由于“/some/dir/”(A 和 B 所在的位置)不是“main.php”的当前版本,因此 php 看不到A.php

解决方案:在A.php使用绝对完整路径/some/dir。要么硬编码它,要么通过动态获取它dirname(__FILE__)

# in "A.php"
include dirname(__FILE__) .'/B.php';
于 2010-03-13T14:43:24.413 回答