Implementation of the PHP class

Source: Internet
Author: User
Tags php class php script

The artifacts of the class compilation phase in 1.PHP, and objects are generated at runtime, and they fall into different stages. A class can contain constants, variables (called "Attributes") and functions (called "methods") that belong to them.

2. First we look at the data structure of the class:

struct_zend_class_entry {CharType//type of class: Inner class Zend_internal_class (1), user-defined class Zend_user_class (2)Zend_string *name;//class name, PHP class is case-insensitive, uniform is lowercase    struct_zend_class_entry *parent;//Parent Class    intRefCount;  uint32_t ce_flags; //class masks, such as ordinary classes, abstract classes, interfaces, except that there are other meanings, not yet clear    intDefault_properties_count;//Normal attribute count, including public, private    intDefault_static_members_count;//static number of properties, staticsZval *default_properties_table;//an array of common attribute valuesZval *default_static_members_table;//array of static property valuesZval *static_members_table;  HashTable function_table; //Member method Hash tableHashTable Properties_info;//member property basic information hash table, key is member name, value is Zend_property_infoHashTable constants_table;//A constant hash table, defined by the constant//here are pointers to constructing correspondence, destructors, magic methodsUnion _zend_function *constructor; Union _zend_function*destructor; Union _zend_function*clone; 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*__debuginfo; Union _zend_function*Serialize_func; Union _zend_function*Unserialize_func;    Zend_class_iterator_funcs Iterator_funcs; //custom hook functions, usually used when defining internal classes, can be flexibly performed in a number of individual operations//user-defined classes are not used, temporarily ignoredzend_object* (*create_object) (Zend_class_entry *Class_type); Zend_object_iterator* (*get_iterator) (Zend_class_entry *ce, Zval *Object,intby_ref); int(*interface_gets_implemented) (Zend_class_entry *iface, Zend_class_entry *class_type);/*A class implements this interface*/Union _zend_function* (*get_static_method) (Zend_class_entry *ce, zend_string*method); /*Serializer Callbacks*/    int(*serialize) (Zval *Object, unsignedChar**buffer, size_t *buf_len, Zend_serialize_data *data); int(*unserialize) (Zval *Object, Zend_class_entry *ce,ConstUnsignedChar*buf, size_t Buf_len, Zend_unserialize_data *data); uint32_t num_interfaces; //number of interfaces implementeduint32_t num_traits; Zend_class_entry**interfaces;//the implemented interfaceZend_class_entry**traits; Zend_trait_alias**trait_aliases; Zend_trait_precedence**trait_precedences; Union {struct{zend_string*filename;            uint32_t Line_start;            uint32_t line_end; Zend_string*doc_comment;        } user; struct {            Const struct_zend_function_entry *builtin_functions; struct_zend_module_entry *module;//owning extension}Internal; } info;

Create_object to instantiate an object, you can extend a custom function to take over the operation of the instantiated object, and without defining the function it will be handled by default zend_objects_new() , and you can refer to the implementation of this function when customizing:

// Note: This action does not copy the attribute to Zend_object: Completed by Object_properties_init () Zend_api zend_object *zend_objects_new (zend_class_entry *CE) {    *Object = Emalloc (sizeof (zend_object) + zend_object_properties_size (CE));    Zend_object_std_init (object, CE);     // set the handler    of an object operation object->handlers = &std_object_handlers;     return Object ;}

For example, to define a user class, which inherits the human class, has a constant, a static property, and two common attributes in the user class:

//Parent ClassclassHuman {}classUser extends human{ConstType = the; Static$name ="UUU";  Public$uid = the;  Public$sex ='W';  Publicfunction __construct () {} Publicfunction GetName () {return$ This-name; }}

Its corresponding zend_class_entry storage structure, such as

At the beginning, it has been mentioned that the class is the product of the compile phase, and each class that we define after compiling compiles a zend_class_entry, which holds all the information about the class, and this structure is used for all class-related operations during the execution phase.

The classes defined in the PHP script and the inner classes defined in the kernel and extension are stored as a hash table indexed by the "class name",EG (class_table).

A constant constant is the data of a class dimension (not the object), which is zend_class_entry.constants_table stored by a hash structure, indexed by a constant name , and value is a defined constant value. The constant name is saved in literals (its op_type = Is_const), and the constant name is taken out on execution, and then the zend_class_entry.constants_table hash table is indexed to the specific constant value.

Member property is divided into two categories: normal and static . Static properties are declared through static by self :: $property or class name:: $property Access, normal property by $this->property or $object->property access.

The initialization value of a member property is not stored directly as a hash table indexed by the property name, but is saved by an array, with an array of normal and static properties stored separately

Access is still based on a hash list indexed by the "property name" to find the specific value, the hash table is not according to the normal properties, static attributes are divided into two, but only one:HashTable properties_info . The value type of this hashtable storage element is zend_property_info .

typedefstruct_zend_property_info {uint32_t offset;//Memory offset value for normal member variables//array index of static member variablesuint32_t flags;//property masks, such as public, private, protected, and whether static propertiesZend_string *name;//Property Name: Not the original property nameZend_string *doc_comment; Zend_class_entry*ce;//Owning class} zend_property_info;//Flags Identify bits#defineZend_acc_public 0x100#defineZend_acc_protected 0x200#defineZend_acc_private 0x400#defineZend_acc_static 0x01

Member method member methods also have static, non-static points, and $this cannot be used in static methods because the scope of their operations is all classes rather than objects, while non-static methods can access member properties of this object through $this.

The process of compiling a class, mainly around Zend_class_entry, the procedure of class inserting eg (class_table) is equivalent to "executing" the declaration of the class in advance of the compilation phase, or it may be deferred to runtime execution because the parent class cannot find the reason.

The implementation of an inner class is to create a zend_class_entry structure and then insert it into eg (class_table).

Implementation of the PHP class

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.