A library for handling PHP errors and exceptions in a better way.

v1.0.9 2024-01-08 22:53 UTC

This package is auto-updated.

Last update: 2024-04-08 23:24:48 UTC


README

A library for handling PHP errors and exceptions in a better way.

badge.svg?branch=main 68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d57656246696f72695f657272266d65747269633d616c6572745f737461747573 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77656266696f72692f6572723f636f6c6f723d6c696768742d677265656e

Supported PHP Versions

Build Status
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main

Installation

The library can be included in your project by including following entry in your require section of your composer.json: "webfiori/err":"*".

Features

  • Conversion of all PHP errors to exceptions.
  • Ability to create a custom exceptions' handler.
  • Provides OOP abstraction for the function set_exception_handler()

Usage

The library has two main classes that the developer will work with. The first one is the class Handler and the second class is AbstractExceptionHandler. The first class is the core of the library. It is used to set custom exception handler. The second class is used to implement custom exception handler. Since the library will convert all PHP errors to exceptions, no need for the developer to have a custom errors handler.

Implementing a Custom Exceptions Handler

First step in setting a custom exceptions handler is to implement one. Implementing a custom handler is strait forward procedure. Simply, the developer have to extend the class AbstractExceptionHandler and implement one abstract method of the class. The method AbstractExceptionHandler::handle() is used to handle the exception. The developer can have access to the properties of the thrown exception in order to handle it properly. The library comes with one default exceptions handler which can act as good example in how to implement a custom handler.

<?php
namespace webfiori\error;

class DefaultExceptionsHandler extends AbstractExceptionHandler {
    public function __construct() {
        parent::__construct();
    }
    /**
     * Handles the exception.
     */
    public function handle() {
        echo '<pre>';
        echo 'An exception was thrown at '.$this->getClass().' line '.$this->getLine().'.<br>';
        echo 'Exception message: '.$this->getMessage().'.<br>';
        echo 'Stack trace:<br>';
        $trace = $this->getTrace();
        
        if (count($trace) == 0) {
            echo '&lt;Empty&gt;';
        } else {
            $index = '0';

            foreach ($trace as $entry) {
                echo '#'.$index.' '.$entry.'<br>';
                $index++;
            }
        }
        echo '</pre>';
    }

    public function isActive(): bool {
        //Activate or deactivate the handler based on conditions.
        return true;
    }

    public function isShutdownHandler(): bool {
        //Set the handler as shutdown handler (errors after processing)
        return false;
    }
}

Setting a Custom Exceptions Handler

After implementing the handler, the developer must set it as exceptions handler. To do that, simply the developer have to use the method Handler::setHandler() in any place in his source code.

Handler::setHandler(new DefaultExceptionsHandler());