summaryrefslogtreecommitdiff
path: root/Zend/zend_inheritance.c
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-03-04 10:10:36 +0100
committerNikita Popov <nikita.ppv@gmail.com>2020-03-04 10:29:21 +0100
commitc05a9c3dcd587418906d9d25f83872e027f7dd43 (patch)
tree7c4ab9a194c44cf75c504634a8b576875ec3f9ab /Zend/zend_inheritance.c
parenta527c60ede98d51785091ceb380242b0ffeab5ad (diff)
downloadphp-git-c05a9c3dcd587418906d9d25f83872e027f7dd43.tar.gz
Implement interfaces after all methods available
The place where interface implementation handlers is called is currently ill-defined: If the class implements interfaces itself, the handlers for both the parent interfaces and the new interfaces will be called after all methods are registered (post trait use). If the class does not implement interfaces, then the parent interface handlers are called early during inheritance (before methods are inherited). This commit moves the calls to always occur after all methods are available. For userland classes this will be post trait import, at the time where interfaces get implemented (whether the class itself defines additional interfaces or not). For internal classes it will be at the end of inheritance, as internal class declarations do not have proper finalization. This allows us to simplify the logic for implementing the magic Iterator / IteratorAggregate interfaces. In particularly we can now also automatically detect whether an extension of IteratorAggregate can safely reuse a custom get_iterator handler, or whether it needs to switch to the userland mechanism. The Iterator case continues to rely on ZEND_ACC_REUSE_GET_ITERATOR for this purpose, as a wholesale replacement is not possible there.
Diffstat (limited to 'Zend/zend_inheritance.c')
-rw-r--r--Zend/zend_inheritance.c30
1 files changed, 12 insertions, 18 deletions
diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c
index f15c325d67..a031207b85 100644
--- a/Zend/zend_inheritance.c
+++ b/Zend/zend_inheritance.c
@@ -141,10 +141,6 @@ static void do_inherit_parent_constructor(zend_class_entry *ce) /* {{{ */
if (EXPECTED(!ce->get_iterator)) {
ce->get_iterator = parent->get_iterator;
}
- if (parent->iterator_funcs_ptr) {
- /* Must be initialized through iface->interface_gets_implemented() */
- ZEND_ASSERT(ce->iterator_funcs_ptr);
- }
if (EXPECTED(!ce->__get)) {
ce->__get = parent->__get;
}
@@ -1185,19 +1181,6 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
ce->parent = parent_ce;
ce->ce_flags |= ZEND_ACC_RESOLVED_PARENT;
- /* Inherit interfaces */
- if (parent_ce->num_interfaces) {
- if (!ce->num_interfaces) {
- zend_do_inherit_interfaces(ce, parent_ce);
- } else {
- uint32_t i;
-
- for (i = 0; i < parent_ce->num_interfaces; i++) {
- do_implement_interface(ce, parent_ce->interfaces[i]);
- }
- }
- }
-
/* Inherit properties */
if (parent_ce->default_properties_count) {
zval *src, *dst, *end;
@@ -1378,6 +1361,10 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
do_inherit_parent_constructor(ce);
if (ce->type == ZEND_INTERNAL_CLASS) {
+ if (parent_ce->num_interfaces) {
+ zend_do_inherit_interfaces(ce, parent_ce);
+ }
+
if (ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
}
@@ -1533,7 +1520,9 @@ static void zend_do_implement_interfaces(zend_class_entry *ce, zend_class_entry
ce->interfaces = interfaces;
ce->ce_flags |= ZEND_ACC_RESOLVED_INTERFACES;
- i = num_parent_interfaces;
+ for (i = 0; i < num_parent_interfaces; i++) {
+ do_implement_interface(ce, ce->interfaces[i]);
+ }
/* Note that new interfaces can be added during this loop due to interface inheritance.
* Use num_interfaces rather than ce->num_interfaces to not re-process the new ones. */
for (; i < num_interfaces; i++) {
@@ -2459,6 +2448,8 @@ ZEND_API int zend_do_link_class(zend_class_entry *ce, zend_string *lc_parent_nam
}
if (interfaces) {
zend_do_implement_interfaces(ce, interfaces);
+ } else if (parent && parent->num_interfaces) {
+ zend_do_inherit_interfaces(ce, parent);
}
if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
zend_verify_abstract_class(ce);
@@ -2542,6 +2533,9 @@ zend_bool zend_try_early_bind(zend_class_entry *ce, zend_class_entry *parent_ce,
}
}
zend_do_inheritance_ex(ce, parent_ce, status == INHERITANCE_SUCCESS);
+ if (parent_ce && parent_ce->num_interfaces) {
+ zend_do_inherit_interfaces(ce, parent_ce);
+ }
zend_build_properties_info_table(ce);
if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
zend_verify_abstract_class(ce);