]> granicus.if.org Git - php/commitdiff
Make constant() error handling consistent with plain const lookup
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 10 Jan 2020 11:01:42 +0000 (12:01 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 10 Jan 2020 12:39:56 +0000 (13:39 +0100)
This means we get an Error exception and a much better error
message indicating the root cause (e.g. accessing a private class
constant).

Zend/tests/018.phpt
Zend/tests/bug51791.phpt
ext/standard/basic_functions.c
ext/standard/tests/general_functions/bug72920.phpt
tests/classes/constants_visibility_002.phpt
tests/classes/constants_visibility_003.phpt
tests/lang/bug44827.phpt

index 97e53b105008f043efffc8bec45ba85cc48cadc5..74c5edcb1fc4e903f03fb3acf6d777c3cc99ad68 100644 (file)
@@ -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
index 0b92fa8c62592f0e31ccdf4273db32840a1c77d9..beb50bad909c2c237bd4208bb8ee07f558d2ceca 100644 (file)
@@ -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'
index ef903be1e9c5b61728c04e545f2cb1e050129c41..860c94acfbcf41b8da888863d8759758b1711953 100755 (executable)
@@ -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;
-                       }
-               }
-       } else {
-               if (!EG(exception)) {
-                       php_error_docref(NULL, E_WARNING, "Couldn't find constant %s", ZSTR_VAL(const_name));
+       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();
                }
-               RETURN_NULL();
        }
 }
 /* }}} */
index 8ba4d26713626a9c3d57d87d5b361d72e798fa00..d3508044fb1d14fbc2f1df3114a2c61baaad06c6 100644 (file)
@@ -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
index 4e0ecb1aa23b8754aec77d399433d6f4812bddab..56c70a99a14a5685e24db08234b00092b5563216 100644 (file)
@@ -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
index 7c961695ed6ce23b31dcba29d12b2f8b2aeb06d4..fe9bb6f6a11941b057c44487209c5c160bed2f5b 100644 (file)
@@ -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
index d871cfd2a89fca5ca312480f147a02d3d0705039..9cb8c132e784716b1f7db0fbde57f69c15d7d0ed 100644 (file)
@@ -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