Browse Source

MFH: do not allow to implement the same interface twice

add tests
PECL_OPENSSL
Antony Dovgal 20 years ago
parent
commit
8204511d71
  1. 15
      Zend/tests/objects_012.phpt
  2. 15
      Zend/tests/objects_013.phpt
  3. 15
      Zend/tests/objects_014.phpt
  4. 11
      Zend/zend_compile.c

15
Zend/tests/objects_012.phpt

@ -0,0 +1,15 @@
--TEST--
implementing a class
--FILE--
<?php
class foo {
}
interface bar extends foo {
}
echo "Done\n";
?>
--EXPECTF--
Fatal error: bar cannot implement foo - it is not an interface in %s on line %d

15
Zend/tests/objects_013.phpt

@ -0,0 +1,15 @@
--TEST--
implementing the same interface twice
--FILE--
<?php
interface foo {
}
class bar implements foo, foo {
}
echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot implement previously implemented interface foo in %s on line %d

15
Zend/tests/objects_014.phpt

@ -0,0 +1,15 @@
--TEST--
extending the same interface twice
--FILE--
<?php
interface foo {
}
interface bar extends foo, foo {
}
echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot implement previously implemented interface foo in %s on line %d

11
Zend/zend_compile.c

@ -2247,6 +2247,17 @@ static zend_bool do_inherit_constant_check(HashTable *child_constants_table, zva
ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry *iface TSRMLS_DC)
{
zend_uint i, if_num = ce->num_interfaces, cnt = 0;
for (i = 0; i < if_num; i++) {
if (ce->interfaces[i] != NULL && ce->interfaces[i]->name_length == iface->name_length && !memcmp(ce->interfaces[i]->name, iface->name, iface->name_length)) {
cnt++;
if (cnt > 1) {
zend_error(E_COMPILE_ERROR, "Cannot implement previously implemented interface %s", iface->name);
}
}
}
zend_hash_merge_ex(&ce->constants_table, &iface->constants_table, (copy_ctor_func_t) zval_add_ref, sizeof(zval *), (merge_checker_func_t) do_inherit_constant_check, iface);
zend_hash_merge_ex(&ce->function_table, &iface->function_table, (copy_ctor_func_t) do_inherit_method, sizeof(zend_function), (merge_checker_func_t) do_inherit_method_check, ce);

Loading…
Cancel
Save