Skip to content

[RFC] Add get_error_handler(), get_exception_handler() functions #17693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Zend/tests/get_error_handler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--TEST--
get_error_handler()
--FILE--
<?php

class C {
function handle() {}
static function handleStatic() {}
}

class Invokable {
public function __invoke() {
}
}

function foo() {}

echo "No error handler\n";
var_dump(get_error_handler() === null);

echo "\nFunction string\n";
set_error_handler('foo');
var_dump(get_error_handler() === 'foo');

echo "\nNULL\n";
set_error_handler(null);
var_dump(get_error_handler() === null);

echo "\nStatic method array\n";
set_error_handler([C::class, 'handleStatic']);
var_dump(get_error_handler() === [C::class, 'handleStatic']);

echo "\nStatic method string\n";
set_error_handler('C::handleStatic');
var_dump(get_error_handler() === 'C::handleStatic');

echo "\nInstance method array\n";
set_error_handler([$c = new C(), 'handle']);
var_dump(get_error_handler() === [$c, 'handle']);

echo "\nFirst class callable method\n";
set_error_handler($f = (new C())->handle(...));
var_dump(get_error_handler() === $f);

echo "\nClosure\n";
set_error_handler($f = function () {});
var_dump(get_error_handler() === $f);

echo "\nInvokable\n";
set_error_handler($object = new Invokable());
var_dump(get_error_handler() === $object);

echo "\nStable return value\n";
var_dump(get_error_handler() === get_error_handler());

?>
==DONE==
--EXPECT--
No error handler
bool(true)

Function string
bool(true)

NULL
bool(true)

Static method array
bool(true)

Static method string
bool(true)

Instance method array
bool(true)

First class callable method
bool(true)

Closure
bool(true)

Invokable
bool(true)

Stable return value
bool(true)
==DONE==
87 changes: 87 additions & 0 deletions Zend/tests/get_exception_handler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--TEST--
get_exception_handler()
--FILE--
<?php

class C {
function handle() {}
static function handleStatic() {}
}

class Invokable {
public function __invoke() {
}
}

function foo() {}

echo "No exception handler\n";
var_dump(get_exception_handler() === null);

echo "\nFunction string\n";
set_exception_handler('foo');
var_dump(get_exception_handler() === 'foo');

echo "\nNULL\n";
set_exception_handler(null);
var_dump(get_exception_handler() === null);

echo "\nStatic method array\n";
set_exception_handler([C::class, 'handleStatic']);
var_dump(get_exception_handler() === [C::class, 'handleStatic']);

echo "\nStatic method string\n";
set_exception_handler('C::handleStatic');
var_dump(get_exception_handler() === 'C::handleStatic');

echo "\nInstance method array\n";
set_exception_handler([$c = new C(), 'handle']);
var_dump(get_exception_handler() === [$c, 'handle']);

echo "\nFirst class callable method\n";
set_exception_handler($f = (new C())->handle(...));
var_dump(get_exception_handler() === $f);

echo "\nClosure\n";
set_exception_handler($f = function () {});
var_dump(get_exception_handler() === $f);

echo "\nInvokable\n";
set_exception_handler($object = new Invokable());
var_dump(get_exception_handler() === $object);

echo "\nStable return value\n";
var_dump(get_exception_handler() === get_exception_handler());

?>==DONE==
--EXPECT--
No exception handler
bool(true)

Function string
bool(true)

NULL
bool(true)

Static method array
bool(true)

Static method string
bool(true)

Instance method array
bool(true)

First class callable method
bool(true)

Closure
bool(true)

Invokable
bool(true)

Stable return value
bool(true)
==DONE==
18 changes: 18 additions & 0 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,15 @@ ZEND_FUNCTION(restore_error_handler)
}
/* }}} */

ZEND_FUNCTION(get_error_handler)
{
ZEND_PARSE_PARAMETERS_NONE();

if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
RETURN_COPY(&EG(user_error_handler));
}
}

/* {{{ Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
ZEND_FUNCTION(set_exception_handler)
{
Expand Down Expand Up @@ -1369,6 +1378,15 @@ ZEND_FUNCTION(restore_exception_handler)
}
/* }}} */

ZEND_FUNCTION(get_exception_handler)
{
ZEND_PARSE_PARAMETERS_NONE();

if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
RETURN_COPY(&EG(user_exception_handler));
}
}

static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags) /* {{{ */
{
zend_string *key;
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ function set_error_handler(?callable $callback, int $error_levels = E_ALL) {}

function restore_error_handler(): true {}

function get_error_handler(): ?callable {}

/** @return callable|null */
function set_exception_handler(?callable $callback) {}

function restore_exception_handler(): true {}

function get_exception_handler(): ?callable {}

/**
* @return array<int, string>
* @refcount 1
Expand Down
11 changes: 10 additions & 1 deletion Zend/zend_builtin_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading