Skip to content

Commit 7ce531f

Browse files
committed
Make constant() error handling consistent with plain const lookup
This means we get an Error exception and a much better error message indicating the root cause (e.g. accessing a private class constant).
1 parent 0a2f6c5 commit 7ce531f

File tree

7 files changed

+51
-35
lines changed

7 files changed

+51
-35
lines changed

Zend/tests/018.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ constant() tests
33
--FILE--
44
<?php
55

6-
var_dump(constant(""));
6+
try {
7+
var_dump(constant(""));
8+
} catch (Error $e) {
9+
echo $e->getMessage(), "\n";
10+
}
711

812
define("TEST_CONST", 1);
913
var_dump(constant("TEST_CONST"));
@@ -13,9 +17,8 @@ var_dump(constant("TEST_CONST2"));
1317

1418
echo "Done\n";
1519
?>
16-
--EXPECTF--
17-
Warning: constant(): Couldn't find constant in %s on line %d
18-
NULL
20+
--EXPECT--
21+
Undefined constant ''
1922
int(1)
2023
string(4) "test"
2124
Done

Zend/tests/bug51791.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ Bug #51791 (constant() failed to check undefined constant and php interpreter st
66
class A {
77
const B = 1;
88
}
9-
var_dump(constant('A::B1'));
9+
try {
10+
constant('A::B1');
11+
} catch (Error $e) {
12+
echo $e->getMessage(), "\n";
13+
}
1014

1115
?>
12-
--EXPECTF--
13-
Warning: constant(): Couldn't find constant A::B1 in %s on line %d
14-
NULL
16+
--EXPECT--
17+
Undefined class constant 'A::B1'

ext/standard/basic_functions.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,19 +1285,16 @@ PHP_FUNCTION(constant)
12851285
ZEND_PARSE_PARAMETERS_END();
12861286

12871287
scope = zend_get_executed_scope();
1288-
c = zend_get_constant_ex(const_name, scope, ZEND_FETCH_CLASS_SILENT);
1289-
if (c) {
1290-
ZVAL_COPY_OR_DUP(return_value, c);
1291-
if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
1292-
if (UNEXPECTED(zval_update_constant_ex(return_value, scope) != SUCCESS)) {
1293-
return;
1294-
}
1295-
}
1296-
} else {
1297-
if (!EG(exception)) {
1298-
php_error_docref(NULL, E_WARNING, "Couldn't find constant %s", ZSTR_VAL(const_name));
1288+
c = zend_get_constant_ex(const_name, scope, 0);
1289+
if (!c) {
1290+
RETURN_THROWS();
1291+
}
1292+
1293+
ZVAL_COPY_OR_DUP(return_value, c);
1294+
if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
1295+
if (UNEXPECTED(zval_update_constant_ex(return_value, scope) != SUCCESS)) {
1296+
RETURN_THROWS();
12991297
}
1300-
RETURN_NULL();
13011298
}
13021299
}
13031300
/* }}} */

ext/standard/tests/general_functions/bug72920.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ class Foo {
66
private const C1 = "a";
77
}
88

9-
var_dump(constant('Foo::C1'));
10-
--EXPECTF--
11-
Warning: constant(): Couldn't find constant Foo::C1 in %s on line %d
12-
NULL
9+
try {
10+
var_dump(constant('Foo::C1'));
11+
} catch (Error $e) {
12+
echo $e->getMessage(), "\n";
13+
}
14+
?>
15+
--EXPECT--
16+
Cannot access private const Foo::C1

tests/classes/constants_visibility_002.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ class A {
1414

1515
A::staticConstDump();
1616
(new A())->constDump();
17-
constant('A::protectedConst');
17+
try {
18+
constant('A::protectedConst');
19+
} catch (Error $e) {
20+
echo $e->getMessage(), "\n";
21+
}
1822

1923
?>
20-
--EXPECTF--
24+
--EXPECT--
2125
string(14) "protectedConst"
2226
string(14) "protectedConst"
23-
24-
Warning: constant(): Couldn't find constant A::protectedConst in %s on line %d
27+
Cannot access protected const A::protectedConst

tests/classes/constants_visibility_003.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ class A {
1414

1515
A::staticConstDump();
1616
(new A())->constDump();
17-
constant('A::privateConst');
17+
try {
18+
constant('A::privateConst');
19+
} catch (Error $e) {
20+
echo $e->getMessage(), "\n";
21+
}
1822

1923
?>
20-
--EXPECTF--
24+
--EXPECT--
2125
string(12) "privateConst"
2226
string(12) "privateConst"
23-
24-
Warning: constant(): Couldn't find constant A::privateConst in %s on line %d
27+
Cannot access private const A::privateConst

tests/lang/bug44827.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ Testfest Munich 2009
77
--FILE--
88
<?php
99
define('::', true);
10-
var_dump(constant('::'));
10+
try {
11+
var_dump(constant('::'));
12+
} catch (Error $e) {
13+
echo $e->getMessage(), "\n";
14+
}
1115
?>
1216
--EXPECTF--
1317
Warning: Class constants cannot be defined or redefined in %s on line %d
1418

15-
Warning: constant(): Couldn't find constant :: in %s on line %d
16-
NULL
19+
Fatal error: Class '' not found in %s on line %d

0 commit comments

Comments
 (0)