Created
November 13, 2021 12:27
-
-
Save carnage/6ac72a505d38be391c825cd4fa31f593 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function spl_push_error_handler(ErrorHandler $handler, int $level = \E_ALL, int $priority = 0): void | |
{ | |
// this function would be built into PHP, possibly with the addition of some extra functions to inspect and remove | |
// existing error handlers | |
// pushed error handlers would take priority over an error handlers set with set_errorhandler(), which would only be called | |
// if the error is not handled by a handler in the stack. | |
// if neither a handler in the stack or the error handler set with set_errorhandler handles the error it performs the default | |
// PHP action of dumping it to screen/logs | |
// A polyfill for previous versions of PHP could be added in userland and set as the error handler with set_errorhandler, | |
// however this suffers from the problem that it might get accidentilly removed by a user setting their own error handler | |
// with that function. | |
} | |
// Also defined as part of the language | |
interface ErrorHandler | |
{ | |
// should return true if the error has been handled by this handler, false otherwise to allow the next handler | |
// in the stack to be invoked. | |
public function handleError(int $errorNumber, string $errorMessage, ErrorContext $errorContext): bool; | |
} | |
// Also defined as part of the langage | |
class ErrorContext | |
{ | |
// file and line are returned as normal for the current error handling | |
public function getLine(): int | |
public function getFile(): string | |
// get class returns fully qualified class name in which the error occured, null if the error was not in a class | |
public function getClass(): ?string | |
// get function returns the fully qualified function name in which the error occurred, null if error was not in a function | |
public function getFunction(): ?string | |
// get method returns the method name the error occurred in, null if not in a method | |
public function getMethod(): ?string | |
// get backtrace returns an error backtrace as if the error were an exception | |
public function getBacktrace(): array | |
} | |
class HandleDepreciations implements ErrorHandler | |
{ | |
public function handleError(int $errorNumber, string $errorMessage, ErrorContext $errorContext): bool | |
{ | |
if (str_starts_with((string) $errorContext->getClass(), 'MY\\NAMESPACE')) { | |
return true; | |
} | |
return false; | |
} | |
} | |
//pushing error handlers could be simplified by a composer plugin if the library has no straight forward entry point | |
spl_push_error_handler(new HandleDepreciations(), \E_DEPRECATED, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment