3

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!

3
  • 2
    I found one for CodeIgniter, called UhOh!. Maybe you can take a look at the code to see how it was done. Commented Mar 16, 2011 at 20:42
  • Good question, lack of decent errors by default is one of PHP's biggest problems IMHO. Commented Mar 16, 2011 at 20:45
  • @Rocket looking at the screenshots, that looks like exactly what I am looking for, thanks Commented Mar 16, 2011 at 21:11

5 Answers 5

6

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.


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()

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, I think you may have misunderstood my question, or I may have not worded it correctly. What I am actually trying to do is show the actual source code from the error line and surrounding lines @ like in Rocket's example link above codeigniter.com/forums/viewthread/160969/P0
2

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 :-)

2 Comments

This is good for a basic error handling but what I am after is actually showing the contents of the line and surrounding lines that the error occurred on like in Rockets link above @ codeigniter.com/forums/viewthread/160969/P0
Right :) A quick way is to just add highlight_file(realpath($filename)); to the above function. A more advanced way is to fopen() and do more fancy stuff like color the line where the error occurred and whatnot ...
1

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.

Comments

0

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

1 Comment

I have and use Xdebug sometimes, however that is not what I am after, I would like to show the source code where the error happened
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.