开发者

Show PHP source code where error occurs?

I once saw a framework or code from some PHP that had an error handler (class or function) that would show an error message, the line number the error occurred on as well as the actual source code, if there was some sort of error in the code, it would show the line or a few lines from the actual PHP file that caused the error. it was really cool, I understand that you would not want to ever do this on a live production server/site but for debugging, it was very nice and something I have never seen before.

开发者_开发技巧I cannot remember where I saw this or how it was done. If you have any ideas on how to do something similar, I would love to have a nice little class for error handling that would do stuff like that, please share any code, ideas, etc on this if you can, really appreciate any help, thank you!


What you are talking of is a stack trace, in PHP you can retrieve it with debug_backtrace().
Most framework have their own way to display stack trace, but basicly they all use such functions.

However, what you are really looking for is Xdebug it is a debugger and a profilter for PHP, and will let you run code step by step.

  • Stack trace with Xdebug

Edit:

If you want to handle error to provide your own way to work with, you have to use set_error_handler() and set_exception_handler()

A simple example:

<?php
    function myErrorHandler($errno, $errstr, $errfile, $errline) {
      echo "[$errno] $errstr" . PHP_EOL;
      echo "On line $errline in file $errfile" . PHP_EOL;

      $range = array(
          $errline - 5,
          $errline + 5,
       );

       $source = explode(PHP_EOL, file_get_contents($errfile));
       for ($i = $range[0]; $i <= $range[1]; ++$i) {
           if ($i === count($source)) break;
           if ($i === $errline-1) {
               printf("%d | %s  <<<<< Here is the error\n", $i, $source[$i]);
           } else {
               printf("%d | %s \n", $i, $source[$i]);
           }
       }
    }

    set_error_handler('myErrorHandler');

    error_reporting(E_ALL);

    $a = 'Setting variable $a';

    $obj = new Stdclass();
    $obj->foo = 'bar';

    // Oops I'm calling an undefined variable
    echo $undefinedVariable;

    $numbers = array(1,2,3,4,5);
    for($i = 0; $i < 5; ++$i) {
        $numbers[$i] = $i - 1;
    }  

Trying this on cli, should show you:

bguery@joyless:sandbox $ php debugbacktrace.php 
[8] Undefined variable: undefinedVariable
On line 33 in file debugbacktrace.php
28 | $obj = new Stdclass(); 
29 | $obj->foo = 'bar'; 
30 |  
31 | // Oops I'm calling an undefined variable 
32 | echo $undefinedVariable;  <<<<< Here is the error
33 |  
34 | $numbers = array(1,2,3,4,5); 
35 | for($i = 0; $i < 5; ++$i) { 
36 |     $numbers[$i] = $i - 1; 
37 | }   
38 |  

Note that's a really basic usage, but it does the trick. You may need to do more work if you intend to handle E_ERROR, E_PARSE, etc. and you may need to use register_shutdown_function()


Here's a simple example.

  set_error_handler('ErrorHandler');

  function ErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
  {
    print '<pre style="line-height: 2em;">';
    printf("==> Error in `%s' line %s: %s\n\n", $filename, $linenum, $errmsg);
    debug_print_backtrace();
    print '</pre>';

    exit($errno);
  }

It's crude and basic, but it's already a lot better than the default IMHO.

At least this gives you a start :-)


Kohana does something like this.

Using the backtrace, you can find the file name and line where calls/errors occured. From there, just open the file and display print out the surrounding lines.


You get this output when you have Xdebug running on your server. (+ the added bonus to have a great debugger for stepping through your php code)

Looks like this:

http://eboraks.com/wp-content/uploads/2009/07/Xdebug_error_message.jpg

more Info on Xdebug:

http://www.xdebug.org/docs/install


As far as frameworks go, both CodeIgniter and CakePHP have error handlers, not sure if you are looking just for a debugger or a framework as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜