Supported Versions: PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8
Sets a user-defined error handler function
<?php set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callable
<?php error_reporting(-1);
set_error_handler($this->forwardsTo('handleError'));
set_exception_handler($this->forwardsTo('handleException'));
<?php }
while (true) {
$previousHandler = set_error_handler(static fn () => null);
restore_error_handler();
<?php protected function withDeprecationHandling()
{
if ($this->originalDeprecationHandler) {
set_error_handler(tap($this->originalDeprecationHandler, fn () => $this->originalDeprecationHandler = null));
}
return $this;
<?php protected function withoutDeprecationHandling()
{
if ($this->originalDeprecationHandler == null) {
$this->originalDeprecationHandler = set_error_handler(function ($level, $message, $file = '', $line = 0) {
if (in_array($level, [E_DEPRECATED, E_USER_DEPRECATED]) || (error_reporting() & $level)) {
throw new ErrorException($message, 0, $level, $file, $line);
}
});
}
return $this;
<?php {
parent::setUp();
set_error_handler(function () {
$this->deprecationsFound = true;
});
}
public function testWithDeprecationHandling()
<?php $value = is_resource($value) ? stream_get_contents($value) : $value;
set_error_handler(function (int $code, string $message): bool {
throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
});
try {
return unserialize($value);
<?php $value = is_resource($value) ? stream_get_contents($value) : $value;
set_error_handler(function (int $code, string $message): bool {
throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
});
try {
return unserialize($value);
<?php public static function register(IOInterface $io = null)
{
set_error_handler(array(__CLASS__, 'handle'));
error_reporting(E_ALL | E_STRICT);
self::$io = $io;
}
<?php if ($copyTo) {
$errorMessage = '';
set_error_handler(function ($code, $msg) use (&$errorMessage) {
if ($errorMessage) {
$errorMessage .= "\n";
}
$errorMessage .= Preg::replace('{^fopen\(.*?\): }', '', $msg);
});
$bodyHandle = fopen($copyTo.'~', 'w+b');
restore_error_handler();
if (!$bodyHandle) {
<?php $errorMessage = '';
$errorCode = 0;
$result = false;
set_error_handler(function ($code, $msg) use (&$errorMessage) {
if ($errorMessage) {
$errorMessage .= "\n";
}
$errorMessage .= Preg::replace('{^file_get_contents\(.*?\): }', '', $msg);
return true;
});
$http_response_header = array();
try {
$result = $this->getRemoteContents($originUrl, $fileUrl, $ctx, $http_response_header, $maxFileSize);
<?php }
$errorMessage = '';
set_error_handler(function ($code, $msg) use (&$errorMessage) {
if ($errorMessage) {
$errorMessage .= "\n";
}
$errorMessage .= Preg::replace('{^file_put_contents\(.*?\): }', '', $msg);
return true;
});
$result = (bool) file_put_contents($fileName, $result);
restore_error_handler();
if (false === $result) {
<?php private static function box(callable $func)
{
self::$lastError = null;
set_error_handler(__CLASS__.'::handleError');
try {
$result = $func(...\array_slice(\func_get_args(), 1));
restore_error_handler();
<?php error_reporting(-1);
set_error_handler([$this, 'renderError']);
set_exception_handler([$this, 'handleException']);
<?php public function toggleCrypto($socket, Deferred $deferred, $toggle, $method)
{
$error = null;
\set_error_handler(function ($_, $errstr) use (&$error) {
$error = \str_replace(array("\r", "\n"), ' ', $errstr);
if (($pos = \strpos($error, "): ")) !== false) {
$error = \substr($error, $pos + 3);
}
});
$result = \stream_socket_enable_crypto($socket, $toggle, $method);
<?php try {
\trigger_error($error, E_USER_ERROR);
} catch (\Throwable $e) {
\set_error_handler(null);
\trigger_error($error, E_USER_ERROR);
}
}