This article mainly introduces about the PHP source code 29: About the interface inheritance, has a certain reference value, now share to everyone, there is a need for friends can refer to
Elementary Introduction to PHP source code 29: About Interface Inheritance
Before we have seen the inheritance of classes in PHP source code, today we look at how the interface inheritance in PHP is implemented.
Again we start looking for an inheritance implementation of the interface from the Cachingiterator class.
Cachingiterator extends Iteratoriterator implements Outeriterator, Traversable, Iterator, arrayaccess, countable
/* Arrayaccess Interface Inheritance Implementation */register_spl_implements (Cachingiterator, arrayaccess); Ext/spl/spl_functions.h 41 Line # define REGISTER_SPL_IMPLEMENTS (Class_name, interface_name) \zend_class_implements ( Spl_ce_ # # Class_name tsrmls_cc, 1, Spl_ce_ # # Interface_name); ZEND/ZEND_API.C 2218 row Zend_api void zend_class_implements (zend_class_entry *class_entry tsrmls_dc, int num_ interfaces, ...) /* {{{*/{zend_class_entry *interface_entry;va_list Interface_list;va_start (interface_list, num_interfaces); while ( num_interfaces--) {interface_entry = Va_arg (interface_list, Zend_class_entry *); Zend_do_implement_interface (Class_ Entry, Interface_entry tsrmls_cc);} Va_end (interface_list);} /*}}} *///zend/zend_complie.c 2887 lines Zend_api void Zend_do_implement_interface (Zend_class_entry *ce, Zend_class_entry * Iface tsrmls_dc)/* {{*/{zend_uint I, ignore = 0;zend_uint Current_iface_num = Ce->num_interfaces;zend_uint parent_if Ace_num = ce->parent? ce->parent->num_interfaces:0; for (i = 0; I &lT ce->num_interfaces; i++) {if (ce->interfaces[i] = = NULL) {memmove (ce->interfaces + i, ce->interfaces + i + 1, sizeof (zend_class_entry *) * (--ce->num_interfaces-i)); i--;} else if (ce->interfaces[i] = = iface) {/* already exists for this interface */if (I < parent_iface_num) {ignore = 1;} else {zend_error (e_compile _error, "Class%s cannot implement previously implemented interface%s", Ce->name, Iface->name);}} if (!ignore) {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));} else {ce->interfaces = (zend_class_entry * *) erealloc (ce->interfaces, sizeof (Zend_class_entry *) * (++current_ Iface_num));}} ce->interfaces[ce->num_interfaces++] = iface;//interface number plus 1, add the interface to the interface list in the/* Merge Interface Constants list and method list */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); Do_implement_interface (CE, iface tsrmls_cc); Zend_do_inherit_interfaces (CE, iface tsrmls_cc);}}
From the constant list and the list of methods in the merge interface of the Zend_do_implement_interface function, we can guess that this may be one of the reasons why the variables can have constants in the interface.
In the process of interface inheritance, there is a judgment operation for the same interface in the interface of the current class, and if the same interface already exists, this interface will not inherit.
The PHP code is as follows:
<?phpinterface foointerface {public Function method1 (),} class Base implements Foointerface {public Function method1 () {echo ' SS ';}} Class Foo extends Base implements Foointerface {} $foo = new Foo (); $foo->method1 ();
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!