Skip to content

Commit 5c329f7

Browse files
committed
Add get_error_handler(), get_exception_handler() functions
1 parent 4fcbdea commit 5c329f7

File tree

5 files changed

+168
-1
lines changed

5 files changed

+168
-1
lines changed

Zend/tests/get_error_handler.phpt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
get_error_handler()
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
function handle() {}
8+
static function handleStatic() {}
9+
}
10+
11+
function foo() {}
12+
13+
set_error_handler("foo");
14+
var_dump(get_error_handler());
15+
16+
set_error_handler(null);
17+
var_dump(get_error_handler());
18+
19+
set_error_handler([C::class, 'handleStatic']);
20+
var_dump(get_error_handler());
21+
22+
set_error_handler('C::handleStatic');
23+
var_dump(get_error_handler());
24+
25+
set_error_handler([new C(), 'handle']);
26+
var_dump(get_error_handler());
27+
28+
set_error_handler((new C())->handle(...));
29+
var_dump(get_error_handler());
30+
31+
set_error_handler(function () {});
32+
var_dump(get_error_handler());
33+
34+
?>
35+
==DONE==
36+
--EXPECTF--
37+
string(3) "foo"
38+
NULL
39+
array(2) {
40+
[0]=>
41+
string(1) "C"
42+
[1]=>
43+
string(12) "handleStatic"
44+
}
45+
string(15) "C::handleStatic"
46+
array(2) {
47+
[0]=>
48+
object(C)#%d (0) {
49+
}
50+
[1]=>
51+
string(6) "handle"
52+
}
53+
object(Closure)#%d (2) {
54+
["function"]=>
55+
string(9) "C::handle"
56+
["this"]=>
57+
object(C)#%d (0) {
58+
}
59+
}
60+
object(Closure)#%d (3) {
61+
["name"]=>
62+
string(%d) "{closure:%s:%d}"
63+
["file"]=>
64+
string(%d) "%s"
65+
["line"]=>
66+
int(%d)
67+
}
68+
==DONE==
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
get_exception_handler()
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
function handle() {}
8+
static function handleStatic() {}
9+
}
10+
11+
function foo() {}
12+
13+
set_exception_handler("foo");
14+
var_dump(get_exception_handler());
15+
16+
set_exception_handler(null);
17+
var_dump(get_exception_handler());
18+
19+
set_exception_handler([C::class, 'handleStatic']);
20+
var_dump(get_exception_handler());
21+
22+
set_exception_handler('C::handleStatic');
23+
var_dump(get_exception_handler());
24+
25+
set_exception_handler([new C(), 'handle']);
26+
var_dump(get_exception_handler());
27+
28+
set_exception_handler((new C())->handle(...));
29+
var_dump(get_exception_handler());
30+
31+
set_exception_handler(function () {});
32+
var_dump(get_exception_handler());
33+
34+
?>
35+
==DONE==
36+
--EXPECTF--
37+
string(3) "foo"
38+
NULL
39+
array(2) {
40+
[0]=>
41+
string(1) "C"
42+
[1]=>
43+
string(12) "handleStatic"
44+
}
45+
string(15) "C::handleStatic"
46+
array(2) {
47+
[0]=>
48+
object(C)#%d (0) {
49+
}
50+
[1]=>
51+
string(6) "handle"
52+
}
53+
object(Closure)#%d (2) {
54+
["function"]=>
55+
string(9) "C::handle"
56+
["this"]=>
57+
object(C)#%d (0) {
58+
}
59+
}
60+
object(Closure)#%d (3) {
61+
["name"]=>
62+
string(%d) "{closure:%s:%d}"
63+
["file"]=>
64+
string(%d) "%s"
65+
["line"]=>
66+
int(%d)
67+
}
68+
==DONE==

Zend/zend_builtin_functions.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,15 @@ ZEND_FUNCTION(restore_error_handler)
13231323
}
13241324
/* }}} */
13251325

1326+
ZEND_FUNCTION(get_error_handler)
1327+
{
1328+
ZEND_PARSE_PARAMETERS_NONE();
1329+
1330+
if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1331+
RETURN_ZVAL(&EG(user_error_handler), 1, 0);
1332+
}
1333+
}
1334+
13261335
/* {{{ Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
13271336
ZEND_FUNCTION(set_exception_handler)
13281337
{
@@ -1369,6 +1378,15 @@ ZEND_FUNCTION(restore_exception_handler)
13691378
}
13701379
/* }}} */
13711380

1381+
ZEND_FUNCTION(get_exception_handler)
1382+
{
1383+
ZEND_PARSE_PARAMETERS_NONE();
1384+
1385+
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1386+
RETURN_ZVAL(&EG(user_exception_handler), 1, 0);
1387+
}
1388+
}
1389+
13721390
static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags) /* {{{ */
13731391
{
13741392
zend_string *key;

Zend/zend_builtin_functions.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,15 @@ function set_error_handler(?callable $callback, int $error_levels = E_ALL) {}
117117

118118
function restore_error_handler(): true {}
119119

120+
function get_error_handler(): mixed {}
121+
120122
/** @return callable|null */
121123
function set_exception_handler(?callable $callback) {}
122124

123125
function restore_exception_handler(): true {}
124126

127+
function get_exception_handler(): mixed {}
128+
125129
/**
126130
* @return array<int, string>
127131
* @refcount 1

Zend/zend_builtin_functions_arginfo.h

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)