PHP code example of pointybeard / helpers-exceptions-readabletrace

1. Go to this page and download the library: Download pointybeard/helpers-exceptions-readabletrace library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pointybeard / helpers-exceptions-readabletrace example snippets




declare(strict_types=1);
ons\ReadableTrace\ReadableTraceException;

class foo
{
    public function __construct()
    {
        // Go a little deeper so there is more to show in the backtrace
        $this->someMethod();
    }

    private function someMethod()
    {
        // Do some work. Trigger an error.
        throw new ReadableTraceException('Oh oh, something went wrong.');
    }
}

try {
    // Do some work here
    $f = new Foo();
} catch (ReadableTraceException $ex) {
    // Template for displaying the exception to the screen
    $message = <<<OUTPUT
[%s]
An error occurred around line %d of %s. The following was returned:

%s

Backtrace
==========
%s

OUTPUT;

    printf(
        $message,
        (new \ReflectionClass($ex))->getName(),
        $ex->getLine(),
        $ex->getFile(),
        $ex->getMessage(),
        $ex->getReadableTrace()
    );
    // [pointybeard\Helpers\Exceptions\ReadableTrace\ReadableTraceException]
    // An error occurred around line 18 of /path/to/test.php. The following was returned:
    //
    // Oh oh, something went wrong.
    //
    // Backtrace
    // ==========
    // [test.php:12] foo->someMethod();
    // [test.php:24] foo->__construct();
}