In-depth understanding of the PHP kernel (14) class member variables and methods, in-depth understanding of the kernel _php tutorial

Source: Internet
Author: User
Tags php source code vars

In-depth understanding of the member variables and methods of the PHP kernel (14) class, in-depth understanding of the kernel


Original link: http://www.orlion.ga/1237/

The member variables of a class are essentially a variable in PHP, except that they belong to a class and have access control for those variables.

The member method of a class is essentially a function in PHP, except that the function exists as a class method, which may be a class method or an instance method, and the access control of the class is added to these methods. The member method of a class is an abstraction of real-world entity behavior that can be used to implement the behavior of a class.

First, member variables

Member variables are already registered in the structure of the class at compile time. The Zend_do_begin_class_declaration function is called when the declaration of a class is compiled at compile time. This function is used to initialize the basic information of the class, which includes the member variables of the class. The order of invocation is [ZEND_DO_BEGIN_CLASS_DECLARATION]–>[ZEND_INITALIZE_CLASS_DATA]–>[ZEND_HASH_INIT_EX]

ZEND_HASH_INIT_EX (&ce->default_properties, 0, NULL, Zval_ptr_dtor_func, persistent_hashes, 0);


Because the member variables of a class are saved in Hashtable, the initialization of their data is done using the ZEND_HASH_INIT_EX function.

Initializes the hashtable of the class's member variables when declaring the class, and then zend_do_declare_property at compile time if there is a new member variable property declaration. The function first checks for some cases where member variables are not allowed:

      • Member variables are not allowed in interfaces

      • Member variables cannot have abstract properties

      • Cannot declare member variable to final

      • Properties cannot be declared repeatedly

If you declare a property as final in a class:

Public final $var

Will error: Fatal Error:cannot declare property ... This error is thrown by the Zend_do_declare_property function:

if (Access_type & zend_acc_final) {    zend_error (e_compile_error, "cannot declare property%s::$%s final, the final Modifier is allowed only for methods and classes ",               CG (active_class_entry)->name, var_name-> U.constant.value.str.val);}

After the definition check has no problem, the function initializes the member variable.

Alloc_zval (property);  ¾ęŵif (value) {    //  òʻļļuɩȑďĥ    *property = value->u.constant;} else {    Init_pzval (property);    Z_type_p (property) = Is_null;}

During initialization, the program allocates memory first, if the member variable has initialized data, assigns the data directly to the property, otherwise initializes the Zval and sets its type to Is_null. After the initialization process is complete, the program adds this member variable to the specified class structure by calling the ZEND_DECLARE_PROPERTY_EX function.

The regular member variable is eventually registered to the Default_propertiles field of the class. In our usual work, we may not be able to use the above mentioned procedures, but we may use the Get_class_vars () function to view the class's member variables. This function returns an associative array of the default properties of the class, the elements of which are present in the form of Varname=>value. The implementation core code is as follows:

if (Zend_lookup_class (class_name, Class_name_len, &pce tsrmls_cc) = = FAILURE) {    return_false;} else {    array _init (return_value);    Zend_update_class_constants (*pce tsrmls_cc);    Add_class_vars (*pce, & (*PCE)->default_properties, Return_value tsrmls_cc);    Add_class_vars (*pce, Ce_static_members (*PCE), Return_value tsrmls_cc);}

First call the Zend_lookup_class function to find the class named Class_name and copy it to the PCE variable. The core of this discovery process is a hashtable lookup function Zend_hash_quick_find, which looks for eg (class_table). Determines whether the class exists and returns directly if it exists. If it does not exist, you need to determine whether it can be loaded automatically, and if it can be loaded automatically, the class is loaded and returned. Returns False if the class cannot be found. If a class is found, the returned array is initialized, the static member variable of the class is updated, and the member variable of the class is added to the returned array. There is an updated procedure for the static member variables of the class, and we have a description of the procedure below about static variables.

Second, static member variables

A static member variable of a class is common to all instances and belongs to that class, so it is also called a class variable. In the class structure of PHP, the static variables of the class itself exist in the Default_static_memebers field of the class structure.

Unlike ordinary member variables, class variables can be called directly through the class name, which is also known as the Special class variable. A PHP instance:

Class Tipi {public    static $var = 10;} Tipi:: $var;

See the intermediate code generated by the VLD extension:

function name: (null) Number of ops:6compiled VARs:!0 = $varline # * OP FETCH ext return operands----------------------------------------------------------------                                 -----------------2 0 > ext_stmt 1 NOP 6 2 ext_stmt 3 Zend_fetch_class          : 1 ' Tipi ' 4 fetch_r static member ' var ' 5 > RETURN 1 Branch: # 0; line:2-6; sop:0; Eop:5path #1:0,class Tipi: [no user functions] 

This intermediate code is only associated with tipi:: $var This call corresponds, it does not have much to do with the previous class definition. According to the content generated by VLD we can know the PHP code: TIPI:: $var, the resulting intermediate code includes Zend_fetch_class and Fetch_r. This is just a call to a static variable, but it generates two intermediate codes. Cause: We're going to invoke a static variable of a class, and of course we have to find this class before we get the variable for that class. From the PHP source, this is because at compile time it calls the Zend_do_fetch_static_member function, and in this function called the Zend_do_fetch_class function, which will generate Zend_fetch_class intermediate code. The corresponding execution function is Zend_fetch_class_spec_const_handler. This function calls the Zend_fetch_class function (ZEND/ZEND_EXECUTE_API.C). The Zend_fetch_class function will eventually call the ZEND_LOOKUP_CLASS_EX function to find the class.

The class that is found is then the static member variable of the lookup class whose final function is: Zend_std_get_static_property. This is because the type of the second parameter is Zend_fetch_static_member. This function is finally returned from the Static_members field to find the corresponding value. As before, the Zend_update_class_constant function is executed as before, updating all static member variables of this class, and updating the flowchart with static variables:

Iii. Method of Membership

The Member method is essentially a function, so its storage structure is stored in the zend_function structure as well as regular functions. For multiple member methods of a class, it is a hashtable data structure that stores multiple zend_function structures. As with the previous member variables, the member method also initializes the hashtable of the entire method list by calling the Zend_initalize_class_data method when the class is declared.

Except for the access control keyword, a member method is the same as a regular function, a function called from syntax parsing (all zend_do_begin_function_declaration functions), but its arguments are somewhat different, the third parameter is Is_method, A member method is assigned a value of 1, which represents its properties as a member method. In this function there will be a systematic compilation of judgments, such as the inability to declare private member methods in an interface.
After this procedure is judged, the program adds the method directly to the Function_table field of the class structure, after which there are several compilation tests. For example, some of the magic methods of the interface can not be set to non-public, can not be set to static, such as __call (), __callstatic (), __get () and so on.

As with member variables, the member method also has a function –get_class_methods () that returns all member methods. This function returns an array of the method names defined in the specified class.

Iv. Static Member Methods

A static member method of a class is also often called a class method. Unlike static member variables, static member methods and member methods are stored in the Function_table field of the class structure.

Class tipi{public    static function T () {        echo 1;    }} Tipi::t ();

The above code is part of the intermediate code generated under the VLD extension:

Number of OPS:  8compiled vars:  noneline     # *  op                           fetch          ext  return  Operands---------------------------------------------------------------------------------   2     0  >   ext_stmt         1      NOP   8     2      ext_stmt         3      zend_init_static_method_call                             ' Tipi ', ' t '         4      ext_fcall_begin         5      do_fcall_by_name                              0         6      ext_fcall_end   9     7    > RETURN                                                   1 branch: #  0; line:     2-    9; SOP:     0; EOP:     7path #1:0,class Tipi: Function t:finding Entry Pointsbranch analysis from position:0

As you can see from the above, the invocation of the entire static member method is a process of finding the method before calling it. For the invocation operation, the corresponding intermediate code is zend_init_static_method_call. Since class names and method names are constants, we know that the function that corresponds to the intermediate code is zend_init_static_method_call_spec_const_const_handler. In this function, it calls the Zend_fetch_class function first, finds the class in eg (Class_table) with the class name, and then executes the fetch method of the static method.

if (ce->get_static_method) {    EX (FBC) = Ce->get_static_method (CE, function_name_strval, function_name_ Strlen tsrmls_cc);} else {    EX (FBC) = Zend_std_get_static_method (CE, function_name_strval, Function_name_strlen tsrmls_cc);}

If the Get_static_method method in the class structure exists, call this method, or call Zend_std_get_static_method if it does not exist. In PHP source code Get_static_method method is generally null, here we focus on the Zend_std_get_static_method function. This function looks for the ce->function_table list, checks the access control permissions of the method after finding the method, or returns an error if no access is allowed, otherwise returning the function structure.

http://www.bkjia.com/PHPjc/1115245.html www.bkjia.com true http://www.bkjia.com/PHPjc/1115245.html techarticle In- depth understanding of the PHP kernel (14) class member variables and methods, in-depth understanding of the kernel Source link: http://www.orlion.ga/1237/class member variables in PHP is essentially a variable, but these changes ...

  • 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.