Talking about PHP source code 28: About class structure and inheritance

Source: Internet
Author: User
Tags spl php source code
This article mainly introduces about the PHP source code 28: About the class structure and inheritance, has a certain reference value, now share to everyone, there is a need for friends can refer to

Talking about PHP source code 28: About class structure and inheritance

As a very key and very tangled feature in object-oriented, we need to understand some
In PHP5, from the beginning of the concept of inheritance, today we start from the source of PHP, understand how he achieved.
Before we know the inheritance of classes, we need to know in which way the classes are stored in the PHP source code.
Find Zend/zend.h 418 Line:

 struct _zend_class_entry {char Type;char *name;/* class name */zend_uint name_length;/* class name string length */struct _zend_class_entry *paren T /* Parent class */int RefCount; /* Reference count */zend_bool Constants_updated;zend_uint ce_flags;/* class access control */HashTable function_table;/* class member function */hashtable Default properties of the Default_properties;/* class */hashtable properties_info;/* class property information such as access control */hashtable default_static_members;/* static members list */hashtable *static_members; HashTable constants_table;/* constant list */const struct _zend_function_entry *builtin_functions; Union _zend_function *constructor;/* Constructor */union _zend_function *destructor;/* destructor */union _zend_function *clone;/* Cloning method */* Magic method */union _zend_function *__get;union _zend_function *__set;union _zend_function *__unset;union _zend_ function *__isset;union _zend_function *__call;union _zend_function *__callstatic;union _zend_function *__tostring; Union _zend_function *serialize_func;union _zend_function *unserialize_func; Zend_class_iterator_funcs Iterator_funcs; /* Handlers */zend_object_value (*create_object) (Zend_class_entry *class_type tsrmls_dc); Zend_object_iterator * (*get_iterator) (Zend_class_entry *ce, Zval * object, int by_ref tsrmls_dc); int (*interface_gets_implemented) (Zend_class_entry *iface, Zend_class_entry *class_type TSRMLS_DC); /* A class implements this interface */union _zend_function * (*get_static_method) (Zend_class_entry *ce, char* method, int Method_len tsrmls_dc); /* Serializer callbacks */int (*serialize) (Zval *object, unsigned char **buffer, zend_uint *buf_len, Zend_serialize_data * Data tsrmls_dc); int (*unserialize) (Zval **object, zend_class_entry *ce, const unsigned char *buf, zend_uint Buf_len, Zend_ Unserialize_data *data tsrmls_dc); Zend_class_entry interfaces implemented by the **interfaces;/* class */zend_uint the number of interfaces implemented by the num_interfaces;/* class */* file information */char *filename;zend_uint Line_start;zend_uint Line_end;char *doc_comment;zend_uint Doc_comment_len; struct _zend_module_entry *module;};

From the SPL that comes with PHP, we can see that there are classes of inheritance, starting from here, we can find the implementation process we need

Select the Cachingiterator class as the entry point from the manual.
Cachingiterator extends Iteratoriterator implements Outeriterator, Traversable, Iterator, arrayaccess, countable
The tracing process is as follows:

 In ext/spl/spl_iterators.c 3246 line, REGISTER_SPL_SUB_CLASS_EX (Cachingiterator, Iteratoriterator, Spl_dual_it_new, SPL _funcs_cachingiterator); Ext/spl/spl_functions.h 34 Lines # define REGISTER_SPL_SUB_CLASS_EX (Class_name, Parent_class_name, Obj_ctor, funcs) \ SP L_register_sub_class (&spl_ce_ # # class_name, Spl_ce_ # # Parent_class_name, # class_name, Obj_ctor, Funcs TSRMLS_CC); EXT/SPL/SPL_FUNCTIONS.C 56 row Phpapi void Spl_register_sub_class (zend_class_entry * * ppce, Zend_class_entry * parent_ce , char * class_name, void *obj_ctor, const zend_function_entry * function_list tsrmls_dc) *ppce = Zend_register_internal_cl ASS_EX (&ce, Parent_ce, NULL tsrmls_cc); Zend/zend_api.c 2196 Line Zend_api zend_class_entry *zend_register_internal_class_ex (Zend_class_entry *class_entry, Zend_class_entry *parent_ce, char *parent_name tsrmls_dc) zend_do_inheritance (Register_class, Parent_ce TSRMLS_CC); zend/zend_compile.c 2784 row Zend_api void Zend_do_inheritance (Zend_class_entry *ce, ZEND_CLASs_entry *parent_ce tsrmls_dc) {/* interface cannot inherit class */if ((Ce->ce_flags & Zend_acc_interface) &&! ( Parent_ce->ce_flags & Zend_acc_interface) {zend_error (E_compile_error, "INTERFACE%s may not inherit from Clas    S (%s) ", Ce->name, Parent_ce->name); }/* FINAL class cannot inherit */if (Parent_ce->ce_flags & Zend_acc_final_class) {zend_error (E_compile_error, "Class%    s may isn't inherit from final class (%s) ", Ce->name, Parent_ce->name);    } ce->parent = Parent_ce;    /* Copy serialization and deserialization callback function */if (!ce->serialize) {ce->serialize = parent_ce->serialize;    } if (!ce->unserialize) {ce->unserialize = parent_ce->unserialize;     }/* The interface that inherits the parent class is here to stop and think, why do you do this? */Zend_do_inherit_interfaces (CE, parent_ce tsrmls_cc); /* Inherit the properties of the parent class */Zend_hash_merge (&ce->default_properties, &parent_ce->default_properties, (void *) (void *    )) Zval_add_ref, NULL, sizeof (Zval *), 0); if (parent_ce->type! = ce->type) {/* user-defined class inherits from internal class */zend_update_class_constants (parent_ce tsrmls_cc); Zend_hash_apply_with_arguments (Ce_static_members (Parent_ce) tsrmls_cc, (apply_func_args_t) Inherit_static_prop, 1,    &ce->default_static_members); } else {zend_hash_apply_with_arguments (&parent_ce->default_static_members tsrmls_cc, (apply_func_args_t)    Inherit_static_prop, 1, &ce->default_static_members); } zend_hash_merge_ex (&ce->properties_info, &parent_ce->properties_info, (copy_ctor_func_t) (ce-> Type & Zend_internal_class? Zend_duplicate_property_info_internal:zend_duplicate_property_info), sizeof (Zend_property_info), (merge_checker_     func_t) Do_inherit_property_access_check, CE); /* Merge constants and member functions */Zend_hash_merge (&ce->constants_table, &parent_ce->constants_table, (void *) Zv    Al_add_ref, NULL, sizeof (Zval *), 0); ZEND_HASH_MERGE_EX (&ce->function_table, &parent_ce->function_table, copy_ctor_func_t) Do_inherit_method, sizeof (zend_function), (merge_checker_func_t) Do_inherit_method_check, CE);     Do_inherit_parent_constructor (CE); if (Ce->ce_flags & zend_acc_implicit_abstract_class && ce->type = = zend_internal_class) {Ce->ce_f    Lags |= Zend_acc_explicit_abstract_class; } else if (! ( Ce->ce_flags & Zend_acc_implement_interfaces)) {/* The verification would be do in runtime by Zend_verify_abst    Ract_class */Zend_verify_abstract_class (CE tsrmls_cc); }}

About the above do_inherit_parent_constructor (CE)

This method implements the Magic method inheritance, inheriting the corresponding method of the parent class if there are no related magic methods in the subclass. The PHP code shown below is a subclass without constructors

Class Base {public Function __construct () {    echo ' Base __construct<br/> ';}} class Foo extends Base {} $foo = n EW Foo ();

As above, output: Base __construct
Obviously inherits the constructor of the parent class, and if the subclass has its own constructor and needs to invoke the constructor of the parent class, the constructor of the parent class needs to be called in the constructor of the subclass, and PHP will not be called automatically.

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.