mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
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).
This commit is contained in:
@@ -3,7 +3,11 @@ constant() tests
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
var_dump(constant(""));
|
||||
try {
|
||||
var_dump(constant(""));
|
||||
} catch (Error $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
|
||||
define("TEST_CONST", 1);
|
||||
var_dump(constant("TEST_CONST"));
|
||||
@@ -13,9 +17,8 @@ var_dump(constant("TEST_CONST2"));
|
||||
|
||||
echo "Done\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
Warning: constant(): Couldn't find constant in %s on line %d
|
||||
NULL
|
||||
--EXPECT--
|
||||
Undefined constant ''
|
||||
int(1)
|
||||
string(4) "test"
|
||||
Done
|
||||
|
||||
@@ -6,9 +6,12 @@ Bug #51791 (constant() failed to check undefined constant and php interpreter st
|
||||
class A {
|
||||
const B = 1;
|
||||
}
|
||||
var_dump(constant('A::B1'));
|
||||
try {
|
||||
constant('A::B1');
|
||||
} catch (Error $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Warning: constant(): Couldn't find constant A::B1 in %s on line %d
|
||||
NULL
|
||||
--EXPECT--
|
||||
Undefined class constant 'A::B1'
|
||||
|
||||
@@ -1285,19 +1285,16 @@ PHP_FUNCTION(constant)
|
||||
ZEND_PARSE_PARAMETERS_END();
|
||||
|
||||
scope = zend_get_executed_scope();
|
||||
c = zend_get_constant_ex(const_name, scope, ZEND_FETCH_CLASS_SILENT);
|
||||
if (c) {
|
||||
ZVAL_COPY_OR_DUP(return_value, c);
|
||||
if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
|
||||
if (UNEXPECTED(zval_update_constant_ex(return_value, scope) != SUCCESS)) {
|
||||
return;
|
||||
}
|
||||
c = zend_get_constant_ex(const_name, scope, 0);
|
||||
if (!c) {
|
||||
RETURN_THROWS();
|
||||
}
|
||||
|
||||
ZVAL_COPY_OR_DUP(return_value, c);
|
||||
if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
|
||||
if (UNEXPECTED(zval_update_constant_ex(return_value, scope) != SUCCESS)) {
|
||||
RETURN_THROWS();
|
||||
}
|
||||
} else {
|
||||
if (!EG(exception)) {
|
||||
php_error_docref(NULL, E_WARNING, "Couldn't find constant %s", ZSTR_VAL(const_name));
|
||||
}
|
||||
RETURN_NULL();
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
@@ -6,7 +6,11 @@ class Foo {
|
||||
private const C1 = "a";
|
||||
}
|
||||
|
||||
var_dump(constant('Foo::C1'));
|
||||
--EXPECTF--
|
||||
Warning: constant(): Couldn't find constant Foo::C1 in %s on line %d
|
||||
NULL
|
||||
try {
|
||||
var_dump(constant('Foo::C1'));
|
||||
} catch (Error $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
Cannot access private const Foo::C1
|
||||
|
||||
@@ -14,11 +14,14 @@ class A {
|
||||
|
||||
A::staticConstDump();
|
||||
(new A())->constDump();
|
||||
constant('A::protectedConst');
|
||||
try {
|
||||
constant('A::protectedConst');
|
||||
} catch (Error $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
--EXPECT--
|
||||
string(14) "protectedConst"
|
||||
string(14) "protectedConst"
|
||||
|
||||
Warning: constant(): Couldn't find constant A::protectedConst in %s on line %d
|
||||
Cannot access protected const A::protectedConst
|
||||
|
||||
@@ -14,11 +14,14 @@ class A {
|
||||
|
||||
A::staticConstDump();
|
||||
(new A())->constDump();
|
||||
constant('A::privateConst');
|
||||
try {
|
||||
constant('A::privateConst');
|
||||
} catch (Error $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
--EXPECT--
|
||||
string(12) "privateConst"
|
||||
string(12) "privateConst"
|
||||
|
||||
Warning: constant(): Couldn't find constant A::privateConst in %s on line %d
|
||||
Cannot access private const A::privateConst
|
||||
|
||||
@@ -7,10 +7,13 @@ Testfest Munich 2009
|
||||
--FILE--
|
||||
<?php
|
||||
define('::', true);
|
||||
var_dump(constant('::'));
|
||||
try {
|
||||
var_dump(constant('::'));
|
||||
} catch (Error $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
Warning: Class constants cannot be defined or redefined in %s on line %d
|
||||
|
||||
Warning: constant(): Couldn't find constant :: in %s on line %d
|
||||
NULL
|
||||
Fatal error: Class '' not found in %s on line %d
|
||||
|
||||
Reference in New Issue
Block a user