In the beginning of the first contact with PHP, are process-oriented methods to do some very simple web site play, write PHP code is piling up, scalability and maintenance too bad to change logic is extremely inconvenient. Later found that PHP is supporting object-oriented, suddenly feel that they are really young after that is really ignorant ah, after all, PHP is implemented with C, it is not surprising.
Preface:From our contact
PHPAt first, we first encountered functions: array manipulation functions, string manipulation functions, file manipulation functions, and so on. These functions are the basis for our use of PHP, as well as the process-oriented programming that PHP supports from birth. Process-oriented will be a functional encapsulation, with a modular idea to solve the problem. Support for object-oriented programming from PHP4 onwards. But PHP4-oriented support for the object is not perfect. Since PHP5, PHP has introduced a new object model, which adds a number of new features, including access control, abstract classes and final classes, class methods, magic methods, interfaces, object cloning, and type hints.
and in the recently released PHP5.3 version, increased namespaces for object-oriented programming, delayed static bindingAs well as the addition of two magic Methods __callstatic () and __invoke (). So, at the bottom of PHP, how is it implemented, and how it is structured.
one. Structure of the classA case of reference to Tipi:
Class ParentClass {
}
interface Ifce {public
function IMethod ();
}
Final class Tipi extends ParentClass implements IFCE {public
static $sa = ' AAA ';
Const CA = ' BBB ';
Public Function __constrct () {
} public
function IMethod () {
}
Private Function _access () {
}< C13/>public static function Access () {
}
}
This defines a parent class ParentClass, an interface Ifce, a subclass tipi. The subclass inherits the parent class ParentClass, implements the interface Ifce, and has a static variable $sa, a class constant CA, a common method, a private method, and a public static method. How these structures are implemented within the Zend engine. How the methods of the class and the member variables are stored. Access control, how static members are tagged. First, let's look at the internal storage structure of the class:
struct _zend_class_entry {char type; Type: Zend_internal_class/zend_user_class char *name;//class name Zend_uint name_length; That is, sizeof (name)-1 struct _zend_class_entry *parent; The inherited parent class int refcount;
Reference number Zend_bool constants_updated; Zend_uint Ce_flags; Zend_acc_implicit_abstract_class: Class exists abstract method//Zend_acc_explicit_abstract_class: Add the abstract keyword///before the class name ZEND _acc_final_class//Zend_acc_interface HashTable function_table; Methods HashTable default_properties; Default Properties HashTable Properties_info; The property information HashTable the static variables of the default_static_members;//class itself HashTable *static_members;
Type = = When Zend_user_class, take &default_static_members; When type = = Zend_interal_class, set to null HashTable constants_table;
The constant struct _zend_function_entry *builtin_functions;//method defines the inlet union _zend_function *constructor;
Union _zend_function *destructor; Union _zend_function*clone;
/* 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 *__tostring;
Union _zend_function *serialize_func;
Union _zend_function *unserialize_func; Zend_class_iterator_funcs iterator_funcs;//Iterative/* class handle/Zend_object_value (*create_object) (Zend_class_entry *cl
Ass_type tsrmls_dc);
Zend_object_iterator * (*get_iterator) (Zend_class_entry *ce, Zval *object, Intby_ref tsrmls_dc); /* Class declaration interface */INT (*interface_gets_implemented) (Zend_class_entry *iface, Zend_class_entry *class_type TSRMLS
_DC); /* Serialization callback function pointer/int (*serialize) (Zval *object, Unsignedchar**buffer, Zend_uint *buf_len, Zend_serialize_da
Ta *data tsrmls_dc); Int (*unserialize) (Zval **object, Zend_class_entry *ce, Constunsignedchar*buf, Zend_uint Buf_len, zend_unserial Ize_data *data tsrmls_dc); Zend_class_entry **interfaces; class implements the interface Zend_uint num_interfaces; The number of interfaces implemented by the class char *filename; Class of the storage file address absolute address zend_uint line_start; The start line of the class definition Zend_uint line_end;
The end line Char *doc_comment of the class definition;
Zend_uint Doc_comment_len; struct _zend_module_entry *module;
The module entrance where the class is located: EG (Current_module)};
Take a section of the above structure, we analyze the initial section of the PHP code in the kernel performance. As shown below:
Field name |
Field description |
ParentClass class |
IFCE interface |
Tipi class |
Name |
Class name |
ParentClass |
Ifce |
Tipi |
Type |
Category |
2 (User defined) |
2 (User defined) |
2 (User defined, 1 for System built-in Class) |
Parent |
Parent class |
Empty |
Empty |
ParentClass class |
RefCount |
Reference count |
1 |
1 |
2 |
Ce_flags |
Type of Class |
0 |
144 |
524352 |
Function_table |
List of functions |
Empty |
Function_name=imethod | type=2 | fn_flags=258 |
function_name=__construct | type=2 | fn_flags=8448 Function_name=imethod | type=2 | fn_flags=65800 function_name=_access | type=2 | fn_flags=66560 function_name=access | type=2 | fn_flags=257 |
Interfaces |
Interface List |
Empty |
Empty |
IFCE interface number is 1 |
FileName |
Store file Address |
/tipi.php |
/tipi.php |
/ipi.php |
Line_start |
Class start line Count |
15 |
18 |
22 |
Line_end |
Class end number of rows |
16 |
20 |
38 |
two. Variables and member variablesAs described in the storage mechanism (separation/change) of the PHP kernel, the variable is either defined in the global scope, called a global variable, or defined in a function called a local variable. A member variable is defined within a class and is at the same level as the member method. The following is a simple example of a PHP code that defines a class, and this class has a member variable.
Class Tipi {public
$var;
}
1. Access to member variables: Accessing this member variable is of course accessed through objects. 2. Rules for member variables: 1. Member variable 2 is not allowed in the interface. The member variable cannot have an abstract property of 3. Cannot declare a member variable to final 4. You cannot repeat a property when declaring a class initializes the hashtable of the class's member variable, and then if there is a new member variable declaration, Zend_do_declare_property at compile time. The function first checks the 4 cases that the member variable does not allow. Like what:.
Class Tipi {public
final $var;
}
Run the program will error, violation of the third: Fatal Error:cannot declare property tipi:: $var final, the final modifier are allowed only for methods and C Lasses in.. This error is thrown by the Zend_do_declare_property function
three. Functions and Member MethodsA member method is essentially a function, so its storage structure is stored in the zend_function structure like a regular function.
For multiple member methods of a class, it stores multiple zend_function structures in a hashtable data structure. As with the previous member variable, the member method initializes the hashtable of the entire method list at the time of the class declaration by invoking the Zend_initialize_class_data method. In the class, if we want to define a member method, the format is as follows:
Class tipi{public
function T () {echo 1;}
}
To remove the access control keyword, a member method is the same as a regular function, from a function called in syntax parsing (all zend_do_begin_function_declaration functions), but the parameters of its invocation are somewhat different, and the third parameter Is_method, The member method is assigned a value of 1, which represents its properties as a member method. In this function there will be a system of compiler judgments, such as
cannot declare a private member method in an interface。 Look at this piece of code:
Interface IFCE {
Private Function method ();
}
If you run directly, the program will complain: Fatal error:access type for interface method Ifce::method () must is omitted in this code corresponds to Zend_do_begin_function The code in the _declaration function.
Four. Similarities and differences of methods (function) and functions (method)In the previous introduction of the implementation of functions, functions and methods are more similar in nature, are to put a series of logic in a set to execute, but the two in the use of a lot of differences, here we discuss the implementation of the two. From the point of view of implementation, both internal code is ultimately interpreted as op_array, its implementation is no different (unless the use of $this/self and other specific political reform or methods), and the difference between the two embodied in two aspects: 1. Is the implementation of the definition (registration); 2. Is the implementation of the call;
implementation of the definition (registration) methodBoth functions and methods are registered in the Compiler_globals variable at compile time, both of which use the same kernel-handling functions zend_do_begin_function_declaration () and Zend_do_end_function_ Declaration () to complete the process. The internal content of both is eventually interpreted and stored as a op_codes array, but the location of the "Mount" after compilation is different, as shown in the following figure:
Where functions and methods are registered in PHP
implementation of the calling method
The difference in the definition of location, and the nature of the difference, determines the method than the function to do more validation work, the method call than the function of a call more than one named
Zend_init_method_call's opcode, the function is to register the method with the EXECUTE_DATA.FBC, and then you can use the same handler function as the function
Zend_do_fcall_by_namefor processing.