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 1 commit
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
68 changes: 68 additions & 0 deletions Zend/tests/get_error_handler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
get_error_handler()
--FILE--
<?php

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

function foo() {}

set_error_handler("foo");
var_dump(get_error_handler());

set_error_handler(null);
var_dump(get_error_handler());

set_error_handler([C::class, 'handleStatic']);
var_dump(get_error_handler());

set_error_handler('C::handleStatic');
var_dump(get_error_handler());

set_error_handler([new C(), 'handle']);
var_dump(get_error_handler());

set_error_handler((new C())->handle(...));
var_dump(get_error_handler());

set_error_handler(function () {});
var_dump(get_error_handler());

?>
==DONE==
--EXPECTF--
string(3) "foo"
NULL
array(2) {
[0]=>
string(1) "C"
[1]=>
string(12) "handleStatic"
}
string(15) "C::handleStatic"
array(2) {
[0]=>
object(C)#%d (0) {
}
[1]=>
string(6) "handle"
}
object(Closure)#%d (2) {
["function"]=>
string(9) "C::handle"
["this"]=>
object(C)#%d (0) {
}
}
object(Closure)#%d (3) {
["name"]=>
string(%d) "{closure:%s:%d}"
["file"]=>
string(%d) "%s"
["line"]=>
int(%d)
}
==DONE==
68 changes: 68 additions & 0 deletions Zend/tests/get_exception_handler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
get_exception_handler()
--FILE--
<?php

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

function foo() {}

set_exception_handler("foo");
var_dump(get_exception_handler());

set_exception_handler(null);
var_dump(get_exception_handler());

set_exception_handler([C::class, 'handleStatic']);
var_dump(get_exception_handler());

set_exception_handler('C::handleStatic');
var_dump(get_exception_handler());

set_exception_handler([new C(), 'handle']);
var_dump(get_exception_handler());

set_exception_handler((new C())->handle(...));
var_dump(get_exception_handler());

set_exception_handler(function () {});
var_dump(get_exception_handler());

?>
==DONE==
--EXPECTF--
string(3) "foo"
NULL
array(2) {
[0]=>
string(1) "C"
[1]=>
string(12) "handleStatic"
}
string(15) "C::handleStatic"
array(2) {
[0]=>
object(C)#%d (0) {
}
[1]=>
string(6) "handle"
}
object(Closure)#%d (2) {
["function"]=>
string(9) "C::handle"
["this"]=>
object(C)#%d (0) {
}
}
object(Closure)#%d (3) {
["name"]=>
string(%d) "{closure:%s:%d}"
["file"]=>
string(%d) "%s"
["line"]=>
int(%d)
}
==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_ZVAL(&EG(user_error_handler), 1, 0);
}
}

/* {{{ 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_ZVAL(&EG(user_exception_handler), 1, 0);
}
}

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(): mixed {}

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

function restore_exception_handler(): true {}

function get_exception_handler(): mixed {}

/**
* @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.