]> granicus.if.org Git - php/commitdiff
- Fixed bug #49472 (Constants defined in Interfaces can be overridden)
authorFelipe Pena <felipe@php.net>
Thu, 3 Dec 2009 12:34:50 +0000 (12:34 +0000)
committerFelipe Pena <felipe@php.net>
Thu, 3 Dec 2009 12:34:50 +0000 (12:34 +0000)
Zend/tests/bug49472.phpt [new file with mode: 0644]
Zend/tests/errmsg_025.phpt
Zend/tests/inter_01.phpt
Zend/zend_compile.c

diff --git a/Zend/tests/bug49472.phpt b/Zend/tests/bug49472.phpt
new file mode 100644 (file)
index 0000000..1803d18
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+Bug #49472 (Constants defined in Interfaces can be overridden)
+--FILE--
+<?php
+
+interface ia {
+    const c = 'Sea';
+    const y = 2;
+}
+
+class Foo implements ia {
+}
+
+class FooBar extends Foo implements ia {
+       const x = 1;
+       const c = 'Ocean';
+       
+       public function show() {
+               return ia::c;
+       }
+}
+
+new FooBar;
+
+?>
+--EXPECTF--
+Fatal error: Cannot inherit previously-inherited or override constant c from interface ia in %s on line %d
index 5b4578ac929d80b36375f889ef24520009a96095..014b409479552dda1a4d229d7a2a08c8cfaeacde 100644 (file)
@@ -17,4 +17,4 @@ class test implements test1, test2 {
 echo "Done\n";
 ?>
 --EXPECTF--
-Fatal error: Cannot inherit previously-inherited constant FOO from interface test2 in %s on line %d
+Fatal error: Cannot inherit previously-inherited or override constant FOO from interface test2 in %s on line %d
index db2e86d2067e61cb66067676d3ed7b0bd93d6545..c73397c91afa12bc075fd46047431d6abb4c5f2e 100644 (file)
@@ -15,4 +15,4 @@ class foobar implements foo {
 }
 ?>
 --EXPECTF--
-Fatal error: Cannot inherit previously-inherited constant foo from interface foo in %s on line %d
+Fatal error: Cannot inherit previously-inherited or override constant foo from interface foo in %s on line %d
index 00f46cc37aa8b0bd48c864af9a03276c7f0fb766..0304d6619e38285428a2b161802993a969bee6c9 100644 (file)
@@ -2938,7 +2938,7 @@ static zend_bool do_inherit_constant_check(HashTable *child_constants_table, con
 
        if (zend_u_hash_quick_find(child_constants_table, hash_key->type, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void**)&old_constant) == SUCCESS) {
          if (*old_constant != *parent_constant) {
-                       zend_error(E_COMPILE_ERROR, "Cannot inherit previously-inherited constant %R from interface %v", hash_key->type, hash_key->arKey, iface->name);
+                       zend_error(E_COMPILE_ERROR, "Cannot inherit previously-inherited or override constant %R from interface %v", hash_key->type, hash_key->arKey, iface->name);
                }
                return 0;
        }
@@ -2946,6 +2946,16 @@ static zend_bool do_inherit_constant_check(HashTable *child_constants_table, con
 }
 /* }}} */
 
+static int do_interface_constant_check(zval **val TSRMLS_DC, int num_args, va_list args, const zend_hash_key *key) /* {{{ */
+{
+       zend_class_entry **iface = va_arg(args, zend_class_entry**);
+
+       do_inherit_constant_check(&(*iface)->constants_table, (const zval **) val, key, *iface);
+
+       return ZEND_HASH_APPLY_KEEP;
+}
+/* }}} */
+
 ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry *iface TSRMLS_DC) /* {{{ */
 {
        zend_uint i, ignore = 0;
@@ -2964,7 +2974,10 @@ ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry
                        }
                }
        }
-       if (!ignore) {
+       if (ignore) {
+               /* Check for attempt to redeclare interface constants */
+               zend_hash_apply_with_arguments(&ce->constants_table TSRMLS_CC, (apply_func_args_t) do_interface_constant_check, 1, &iface);
+       } else {
                if (ce->num_interfaces >= current_iface_num) {
                        if (ce->type == ZEND_INTERNAL_CLASS) {
                                ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));