Skip to content

Commit 14fc50e

Browse files
committed
Add get_error_handler(), get_exception_handler() functions
RFC: https://wiki.php.net/rfc/get-error-exception-handler Closes GH-17693
1 parent 5e56c3e commit 14fc50e

7 files changed

+214
-1
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ PHP NEWS
4040
. Fixed bug GH-18026 (Improve "expecting token" error for ampersand). (ilutov)
4141
. Added the (void) cast to indicate that not using a value is intentional.
4242
(timwolla)
43+
. Added get_error_handler(), get_exception_handler() functions. (Arnaud)
4344

4445
- Curl:
4546
. Added curl_multi_get_handles(). (timwolla)

UPGRADING

+6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ PHP 8.5 UPGRADE NOTES
244244
6. New Functions
245245
========================================
246246

247+
- Core:
248+
. get_error_handler() allows retrieving the current user-defined error handler
249+
function
250+
. get_exception_handler() allows retrieving the current user-defined exception
251+
handler function
252+
247253
- Curl:
248254
. curl_multi_get_handles() allows retrieving all CurlHandles current
249255
attached to a CurlMultiHandle. This includes both handles added using

Zend/tests/get_error_handler.phpt

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
--TEST--
2+
get_error_handler()
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
function handle() {}
8+
static function handleStatic() {}
9+
}
10+
11+
class Invokable {
12+
public function __invoke() {
13+
}
14+
}
15+
16+
function foo() {}
17+
18+
echo "No error handler\n";
19+
var_dump(get_error_handler() === null);
20+
21+
echo "\nFunction string\n";
22+
set_error_handler('foo');
23+
var_dump(get_error_handler() === 'foo');
24+
25+
echo "\nNULL\n";
26+
set_error_handler(null);
27+
var_dump(get_error_handler() === null);
28+
29+
echo "\nStatic method array\n";
30+
set_error_handler([C::class, 'handleStatic']);
31+
var_dump(get_error_handler() === [C::class, 'handleStatic']);
32+
33+
echo "\nStatic method string\n";
34+
set_error_handler('C::handleStatic');
35+
var_dump(get_error_handler() === 'C::handleStatic');
36+
37+
echo "\nInstance method array\n";
38+
set_error_handler([$c = new C(), 'handle']);
39+
var_dump(get_error_handler() === [$c, 'handle']);
40+
41+
echo "\nFirst class callable method\n";
42+
set_error_handler($f = (new C())->handle(...));
43+
var_dump(get_error_handler() === $f);
44+
45+
echo "\nClosure\n";
46+
set_error_handler($f = function () {});
47+
var_dump(get_error_handler() === $f);
48+
49+
echo "\nInvokable\n";
50+
set_error_handler($object = new Invokable());
51+
var_dump(get_error_handler() === $object);
52+
53+
echo "\nStable return value\n";
54+
var_dump(get_error_handler() === get_error_handler());
55+
56+
?>
57+
==DONE==
58+
--EXPECT--
59+
No error handler
60+
bool(true)
61+
62+
Function string
63+
bool(true)
64+
65+
NULL
66+
bool(true)
67+
68+
Static method array
69+
bool(true)
70+
71+
Static method string
72+
bool(true)
73+
74+
Instance method array
75+
bool(true)
76+
77+
First class callable method
78+
bool(true)
79+
80+
Closure
81+
bool(true)
82+
83+
Invokable
84+
bool(true)
85+
86+
Stable return value
87+
bool(true)
88+
==DONE==

Zend/tests/get_exception_handler.phpt

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
--TEST--
2+
get_exception_handler()
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
function handle() {}
8+
static function handleStatic() {}
9+
}
10+
11+
class Invokable {
12+
public function __invoke() {
13+
}
14+
}
15+
16+
function foo() {}
17+
18+
echo "No exception handler\n";
19+
var_dump(get_exception_handler() === null);
20+
21+
echo "\nFunction string\n";
22+
set_exception_handler('foo');
23+
var_dump(get_exception_handler() === 'foo');
24+
25+
echo "\nNULL\n";
26+
set_exception_handler(null);
27+
var_dump(get_exception_handler() === null);
28+
29+
echo "\nStatic method array\n";
30+
set_exception_handler([C::class, 'handleStatic']);
31+
var_dump(get_exception_handler() === [C::class, 'handleStatic']);
32+
33+
echo "\nStatic method string\n";
34+
set_exception_handler('C::handleStatic');
35+
var_dump(get_exception_handler() === 'C::handleStatic');
36+
37+
echo "\nInstance method array\n";
38+
set_exception_handler([$c = new C(), 'handle']);
39+
var_dump(get_exception_handler() === [$c, 'handle']);
40+
41+
echo "\nFirst class callable method\n";
42+
set_exception_handler($f = (new C())->handle(...));
43+
var_dump(get_exception_handler() === $f);
44+
45+
echo "\nClosure\n";
46+
set_exception_handler($f = function () {});
47+
var_dump(get_exception_handler() === $f);
48+
49+
echo "\nInvokable\n";
50+
set_exception_handler($object = new Invokable());
51+
var_dump(get_exception_handler() === $object);
52+
53+
echo "\nStable return value\n";
54+
var_dump(get_exception_handler() === get_exception_handler());
55+
56+
?>==DONE==
57+
--EXPECT--
58+
No exception handler
59+
bool(true)
60+
61+
Function string
62+
bool(true)
63+
64+
NULL
65+
bool(true)
66+
67+
Static method array
68+
bool(true)
69+
70+
Static method string
71+
bool(true)
72+
73+
Instance method array
74+
bool(true)
75+
76+
First class callable method
77+
bool(true)
78+
79+
Closure
80+
bool(true)
81+
82+
Invokable
83+
bool(true)
84+
85+
Stable return value
86+
bool(true)
87+
==DONE==

Zend/zend_builtin_functions.c

+18
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,15 @@ ZEND_FUNCTION(restore_error_handler)
13221322
}
13231323
/* }}} */
13241324

1325+
ZEND_FUNCTION(get_error_handler)
1326+
{
1327+
ZEND_PARSE_PARAMETERS_NONE();
1328+
1329+
if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1330+
RETURN_COPY(&EG(user_error_handler));
1331+
}
1332+
}
1333+
13251334
/* {{{ Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
13261335
ZEND_FUNCTION(set_exception_handler)
13271336
{
@@ -1368,6 +1377,15 @@ ZEND_FUNCTION(restore_exception_handler)
13681377
}
13691378
/* }}} */
13701379

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

Zend/zend_builtin_functions.stub.php

+4
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(): ?callable {}
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(): ?callable {}
128+
125129
/**
126130
* @return array<int, string>
127131
* @refcount 1

Zend/zend_builtin_functions_arginfo.h

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)