Is debug_backtrace() safe for serious usage in pro

2019-02-25 01:44发布

问题:

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.

回答1:

debug_backtrace is relatively expensive in my experience, so you should be careful it is not used in loops (e.g. in a custom error handler that catches warnings or notices and performs a backtrace every time).

For any kind of error logging, I think it's pretty invaluable, and because it's going to be called only once, definitely not a performance problem. It is surely always good to include a backtrace in an error report.

I can't see why there would be any specific issues with this function's stability (i.e. calling it causing another crash), I've never heard of any problems. The only "gotcha" I can see is this note in the User Contributed Notes when using objects as function parameters that have no _toString method defined.

Of course, you should never output the results of a backtrace to the end user - that goes without saying.



回答2:

Well, considering its name, I'm not sure I would use it as a "normal" part of my application -- even though I don't remember having read anything which said that it was either good nor bad.


I don't really know what you mean about "serious usage", but :

  • If you need that function for your application to work, it migh indicate some problem in your design
  • This function can be useful in an error-handler, when you want to log how/where an error happened : it will make the log files more useful, when it comes to tracking down the sources of errors

Though, not sure that "error logging" corresponds to your definition of serious usage ?



回答3:

Ok, from my understanding, the problem is following

You've got a php file, let's call it "main.php". In "main.php" you're including "A.php" from some directory:

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

A.php, in turn, includes 'B.php', which is in the same directory as A.php

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

the problem: since "/some/dir/" (where A and B reside) is not the current for "main.php", php does not see B.php from A.php

the solution: in A.php use an absolute full path to /some/dir. Either hardcode it or obtain it dynamically via dirname(__FILE__)

# in "A.php"
include dirname(__FILE__) .'/B.php';