PHP code example of fp / universal-error-catcher

1. Go to this page and download the library: Download fp/universal-error-catcher 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/ */

    

fp / universal-error-catcher example snippets



    $catcher = new UniversalErrorCatcher_Catcher();

    $catcher->registerCallback(function(Exception $e) {
      $to = '[email protected]';
      $subject = 'Error: '.$e->getMessage();
      $body = (string) $e;

      mail($to, $subject, $body);
    });

    $catcher->start();

    // after the start method is called everything is under your control.


    $catcher = new UniversalErrorCatcher_Catcher();

    $catcher->registerCallback(function(Exception $e) {
      $e instanceof FatalErrorException //true
    });

    $catcher->start();

    $anObject->notExistMethod();



    $catcher = new UniversalErrorCatcher_Catcher();

    $catcher->registerCallback(function(Exception $e) {
      $e instanceof FatalErrorException //true
    });

    $catcher->start();

    ini_set('memory_limit', '1K');

    str_repeat('foobar', PHP_INT_MAX);


    $catcher = new UniversalErrorCatcher_Catcher();

    $catcher->registerCallback(function(Exception $e) {
        $e instanceof ErrorException //true
    });

    $catcher->start();
    
    echo $undefinedVariable;
    
    echo 'the script continue to work. This message will be outputed';


    $catcher = new UniversalErrorCatcher_Catcher();
    $catcher->setThrowRecoverableErrors(true); // false by default

    $catcher->registerCallback(function(Exception $e) {
        $e instanceof ErrorException //true
    });

    $catcher->start();
    
    echo $undefinedVariable;
    
    echo 'the exception is throw. It will never be outputed';


    $catcher = new UniversalErrorCatcher_Catcher();
 
    $catcher->registerCallback(function(Exception $e) {
        $e instanceof SuppressedErrorException //true
    });
 
    $catcher->start();

    @trigger_error('supressed warning', E_USER_WARNING);
     
    echo 'the script continue to work. This message will be outputed';


    $catcher = new UniversalErrorCatcher_Catcher();
 
    $catcher->registerCallback(function(Exception $e) {
        $e instanceof LogicException //true
    });
 
    $catcher->start();
 
    throw new LogicException('something strange happened. I am scared.');
     
    echo 'the exception is throw. It will never be outputed';
bash
php composer.phar