From b2186ca7c402da2e8274004dd587b93ac268352a Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 8 Nov 2022 17:13:35 +0100 Subject: [PATCH] Fix GH-9905: constant() behaves inconsistent when class is undefined Directly referring to a constant of an undefined throws an exception; there is not much point in `constant()` raising a fatal error in this case. Closes GH-9907. --- NEWS | 3 +++ ext/standard/basic_functions.c | 2 +- ext/standard/tests/general_functions/gh9905.phpt | 12 ++++++++++++ tests/lang/bug44827.phpt | 3 +-- 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 ext/standard/tests/general_functions/gh9905.phpt diff --git a/NEWS b/NEWS index 28f3de0653b..0022c958e90 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,9 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 8.1.14 +- Core: + . Fixed bug GH-9905 (constant() behaves inconsistent when class is undefined). + (cmb) 24 Nov 2022, PHP 8.1.13 diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 23f736d2009..1e50a37f2c6 100755 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -589,7 +589,7 @@ PHP_FUNCTION(constant) ZEND_PARSE_PARAMETERS_END(); scope = zend_get_executed_scope(); - c = zend_get_constant_ex(const_name, scope, 0); + c = zend_get_constant_ex(const_name, scope, ZEND_FETCH_CLASS_EXCEPTION); if (!c) { RETURN_THROWS(); } diff --git a/ext/standard/tests/general_functions/gh9905.phpt b/ext/standard/tests/general_functions/gh9905.phpt new file mode 100644 index 00000000000..33d2f4f7370 --- /dev/null +++ b/ext/standard/tests/general_functions/gh9905.phpt @@ -0,0 +1,12 @@ +--TEST-- +GH-9905 (constant() behaves inconsistent when class is undefined) +--FILE-- +getMessage()); +} +?> +--EXPECT-- +Class "NonExistantClass" not found diff --git a/tests/lang/bug44827.phpt b/tests/lang/bug44827.phpt index 9220439c884..997a1a633f8 100644 --- a/tests/lang/bug44827.phpt +++ b/tests/lang/bug44827.phpt @@ -21,5 +21,4 @@ try { ?> --EXPECTF-- define(): Argument #1 ($constant_name) cannot be a class constant - -Fatal error: Class "" not found in %s on line %d +Class "" not found