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