]> granicus.if.org Git - php/commitdiff
Fixed bug #75546
authorDanielCiochiu <daniel@ciochiu.ro>
Tue, 12 Feb 2019 10:40:46 +0000 (11:40 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 12 Feb 2019 10:42:33 +0000 (11:42 +0100)
By respecting the SILENT flag when checking the visibility of a
class constant.

NEWS
Zend/zend_constants.c
ext/standard/tests/general_functions/bug72920.phpt
tests/classes/constants_visibility_002.phpt
tests/classes/constants_visibility_003.phpt
tests/classes/constants_visibility_008.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 6fa239021d84b3784952f675cddddc49c534c6f8..2972812f1f3349959cd870a59be259b9ccfeddb4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,8 @@ PHP                                                                        NEWS
 - Core:
   . Fixed bug #77589 (Core dump using parse_ini_string with numeric sections).
     (Laruence)
+  . Fixed bug #75546 (function "defined" should ignore class constant
+    visibility). (Daniel Ciochiu)
 
 - Exif:
   . Fixed bug #77564 (Memory leak in exif_process_IFD_TAG). (Ben Ramsey)
index 14d712235a0c1ee8538b666658da80d2563bf391..64265d3c19c8d5471f64d52a28d69c5ab4f45bb7 100644 (file)
@@ -341,7 +341,9 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
                                ret_constant = NULL;
                        } else {
                                if (!zend_verify_const_access(c, scope)) {
-                                       zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
+                                       if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
+                                               zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
+                                       }
                                        goto failure;
                                }
                                ret_constant = &c->value;
index b5ca4760c307ef96751d46cd846ced8bc3dc3a13..8ba4d26713626a9c3d57d87d5b361d72e798fa00 100644 (file)
@@ -6,10 +6,7 @@ class Foo {
        private const C1 = "a";
 }
 
-try {
-       var_dump(constant('Foo::C1'));
-} catch (Error $e) {
-       var_dump($e->getMessage());
-}
---EXPECT--
-string(35) "Cannot access private const Foo::C1"
+var_dump(constant('Foo::C1'));
+--EXPECTF--
+Warning: constant(): Couldn't find constant Foo::C1 in %s on line %d
+NULL
index 6ec990126942f1cc65722a006df7a2c072e9aa61..4e0ecb1aa23b8754aec77d399433d6f4812bddab 100644 (file)
@@ -21,8 +21,4 @@ constant('A::protectedConst');
 string(14) "protectedConst"
 string(14) "protectedConst"
 
-Fatal error: Uncaught Error: Cannot access protected const A::protectedConst in %s:14
-Stack trace:
-#0 %s(14): constant('A::protectedCon...')
-#1 {main}
-  thrown in %s on line 14
+Warning: constant(): Couldn't find constant A::protectedConst in %s on line %d
index 9c7bcfb21c3f1707ed8f33ac024508734831fb3e..7c961695ed6ce23b31dcba29d12b2f8b2aeb06d4 100644 (file)
@@ -21,8 +21,4 @@ constant('A::privateConst');
 string(12) "privateConst"
 string(12) "privateConst"
 
-Fatal error: Uncaught Error: Cannot access private const A::privateConst in %s:14
-Stack trace:
-#0 %s(14): constant('A::privateConst')
-#1 {main}
-  thrown in %s on line 14
+Warning: constant(): Couldn't find constant A::privateConst in %s on line %d
diff --git a/tests/classes/constants_visibility_008.phpt b/tests/classes/constants_visibility_008.phpt
new file mode 100644 (file)
index 0000000..f24b70c
--- /dev/null
@@ -0,0 +1,12 @@
+--TEST--
+Defined on private constant should not raise exception
+--FILE--
+<?php
+
+class Foo
+{
+    private const BAR = 1;
+}
+echo (int)defined('Foo::BAR');
+--EXPECTF--
+0