Skip to content

Commit b2186ca

Browse files
committed
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.
1 parent e1c52d1 commit b2186ca

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.1.14
44

5+
- Core:
6+
. Fixed bug GH-9905 (constant() behaves inconsistent when class is undefined).
7+
(cmb)
58

69
24 Nov 2022, PHP 8.1.13
710

ext/standard/basic_functions.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ PHP_FUNCTION(constant)
589589
ZEND_PARSE_PARAMETERS_END();
590590

591591
scope = zend_get_executed_scope();
592-
c = zend_get_constant_ex(const_name, scope, 0);
592+
c = zend_get_constant_ex(const_name, scope, ZEND_FETCH_CLASS_EXCEPTION);
593593
if (!c) {
594594
RETURN_THROWS();
595595
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-9905 (constant() behaves inconsistent when class is undefined)
3+
--FILE--
4+
<?php
5+
try {
6+
\constant("\NonExistantClass::non_existant_constant");
7+
} catch (\Throwable|\Error|\Exception $e) {
8+
echo($e->getMessage());
9+
}
10+
?>
11+
--EXPECT--
12+
Class "NonExistantClass" not found

tests/lang/bug44827.phpt

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ try {
2121
?>
2222
--EXPECTF--
2323
define(): Argument #1 ($constant_name) cannot be a class constant
24-
25-
Fatal error: Class "" not found in %s on line %d
24+
Class "" not found

0 commit comments

Comments
 (0)