A flexible PHP error handler that retrieves and filters errors from any object instance based on user access rank.
- Extracts errors from any object that implements
getErrors()or has a public$errorsproperty. - Filters errors by access rank – only shows errors appropriate for the current user.
- Override filtering – force display of all errors or use a custom rank.
- Optional logging – integrates with
ErrorLogto write errors to file. - Strict typing and modern PHP (8.1+).
- PSR‑4 autoloading.
composer require timefrontiers/php-instance-error- PHP 8.1 or higher
- timefrontiers/php-error-log ^1.0 (optional, for logging)
Assume you have a class that collects errors internally:
use TimeFrontiers\InstanceError;
$location = new Location('invalid-ip');
$location->refresh();
// Get all errors (filtered by current user's rank)
$errors = (new InstanceError($location))->get();
// Get errors for a specific context
$errors = (new InstanceError($location))->get('refresh');// Show all errors regardless of rank
$all_errors = (new InstanceError($location, true))->get();
// Use a specific rank for filtering (e.g., developer rank = 7)
$dev_errors = (new InstanceError($location, 7))->get();// Return only the error message strings
$messages = (new InstanceError($location))->get('refresh', true);// Log all errors to a file
(new InstanceError($location))->log();
// Log only errors from a specific context
(new InstanceError($location))->log('refresh', '/path/to/error.log');- The constructor accepts an object instance.
- It extracts errors using:
$instance->getErrors()(preferred modern pattern)- Falls back to
$instance->errors(legacy public property)
- When
get()is called, it determines the user's rank from:- Override value (if
trueor an integer was passed) - Global
$session->access_rank()or$session->access_rank(if available) - Defaults to
0(guest)
- Override value (if
- Only errors where
min_rank <= user_rankare returned.
Errors are expected to be stored as an array with the following structure:
[
'context_name' => [
[$min_rank, $code, $message, $file, $line],
// ...
],
// ...
]| Index | Description |
|---|---|
0 |
Minimum access rank required to view this error |
1 |
Error code (e.g., HTTP status or custom) |
2 |
Human‑readable error message |
3 |
File where error occurred (use __FILE__) |
4 |
Line number (use __LINE__) |
This package is designed to work seamlessly with other TimeFrontiers libraries:
- Session – provides the
access_rankproperty for filtering. - Location – collects errors using
getErrors(). - ErrorLog – writes errors to file when
log()is called.
MIT License. See LICENSE for details.