webservco/error

PHP 组件/库。

v0.8.0 2024-06-22 15:10 UTC

This package is auto-updated.

Last update: 2024-09-15 10:01:32 UTC


README

PHP 组件/库。

自定义应用程序错误处理。

用法

实现接口

ErrorHandlerInterface

interface ErrorHandlerInterface
{
    public function handle(
        // The first parameter, errno, will be passed the level of the error raised, as an integer.
        int $errno,
        // The second parameter, errstr, will be passed the error message, as a string.
        string $errstr,
        // If the callback accepts a third parameter, errfile,
        // it will be passed the filename that the error was raised in, as a string.
        string $errfile,
        // If the callback accepts a fourth parameter, errline,
        // it will be passed the line number where the error was raised, as an integer.
        int $errline,
        // $errcontext removed in PHP 8
    ): bool;
}

ErrorHandlingServiceFactoryInterface

interface ErrorHandlingServiceFactoryInterface
{
    public function createErrorHandlingService(): ErrorHandlingServiceInterface;
}

ErrorHandlingServiceInterface

interface ErrorHandlingServiceInterface
{
    public function handlePreExecutionErrors(): bool;

    public function initialize(): bool;

    public function restore(): bool;
}

示例实现

提供了一个使用严格错误处理器(对任何错误抛出异常)的简单处理服务。

// Create service.
$errorHandlingServiceFactory = new DefaultErrorHandlingServiceFactory();
$errorHandlingService = $errorHandlingServiceFactory->createErrorHandlingService();

// In application bootstrap:
$errorHandlingService->initialize();
$errorHandlingService->handlePreExecutionErrors();

// Application run.

// In application shutdown:
$errorHandlingService->restore();