Skip to content

Commit d0126b8

Browse files
authored
Update README.md
1 parent 21e739c commit d0126b8

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,63 @@ A library for handling PHP errors and exceptions in a better way.
2828
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.0/badge.svg?branch=main"></a>|
2929
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.1/badge.svg?branch=main"></a>|
3030
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.2/badge.svg?branch=main"></a><br>(dev)|
31+
32+
## Installation
33+
The library can be included in your project by including following entry in your `require` section of your `composer.json`: `"webfiori/err":"1.0.0"`.
34+
35+
## Features
36+
* Conversion of all PHP errors to exceptions.
37+
* Ability to create a custom exceptions handler.
38+
* Provides OOP abstraction for the function `set_exception_handler()`
39+
40+
## Usage
41+
42+
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 exceptions handler. Since the library will convert all PHP errors to exceptions, no need for the developer to have a custom errors handler.
43+
44+
### Implementing a Custom Exceptions Handler
45+
46+
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 extends 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.
47+
48+
``` php
49+
<?php
50+
namespace webfiori\error;
51+
52+
class DefaultExceptionsHandler extends AbstractExceptionHandler {
53+
public function __construct() {
54+
parent::__construct();
55+
}
56+
/**
57+
* Handles the exception.
58+
*/
59+
public function handle() {
60+
echo '<pre>';
61+
echo 'An exception was thrown at '.$this->getClass().' line '.$this->getLine().'.<br>';
62+
echo 'Exception message: '.$this->getMessage().'.<br>';
63+
echo 'Stack trace:<br>';
64+
$trace = $this->getTrace();
65+
66+
if (count($trace) == 0) {
67+
echo '&lt;Empty&gt;';
68+
} else {
69+
$index = '0';
70+
71+
foreach ($trace as $entry) {
72+
echo '#'.$index.' '.$entry.'<br>';
73+
$index++;
74+
}
75+
}
76+
echo '</pre>';
77+
}
78+
}
79+
80+
```
81+
82+
### Setting a Custom Exceptions Handler
83+
84+
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.
85+
86+
``` php
87+
Handler::setHandler(new AbstractExceptionHandler());
88+
```
89+
90+

0 commit comments

Comments
 (0)