Array Implementation in PHP Kernel

Source: Internet
Author: User

Arrays are often used in PHP. PHP arrays are powerful and fast. Reading and Writing can be completed in O (1), because each element has the same size, as long as you know the subscript, You can instantly calculate the location of the corresponding element in the memory, so that you can directly retrieve or write it. How is the array implemented in the kernel?

Most PHP functions are implemented through HashTable, which includes arrays.

HashTable has the advantages of two-way linked list, and has the operation performance comparable to that of data.

  • The variables defined in PHP are stored in a symbol table, which is actually a HashTable. every element of PHP is a zval * type variable. In addition, the containers that save user-defined functions, classes, resources, and so on are all implemented in the kernel in the form of HashTable.

    The following describes how to define arrays in PHP and kernel respectively.

    Array definition implementation

    PHP defines the array:

       

    Using Macros in the kernel:

    zval* array;array_init(array);add_assoc_string(array, "key", "value", 1);

    Expand the Macros in the above Code:

    zval* array;ALLOC_INIT_ZVAL(array);Z_TYPE_P(array) = IS_ARRAY;HashTable *h;ALLOC_HASHTABLE(h);Z_ARRVAL_P(array)=h;zend_hash_init(h, 50, NULL,ZVAL_PTR_DTOR, 0);zval* barZval;MAKE_STD_ZVAL(barZval);ZVAL_STRING(barZval, "value", 0);zend_hash_add(h, "key", 4, &barZval, sizeof(zval*), NULL);

    Convenient array macro operations

    The kernel provides us with a convenient macro to manage arrays.

    // Add_assoc _ * series functions: add_assoc_null (zval * aval, char * key); add_assoc_bool (zval * aval, char * key, zend_bool bval); add_assoc_long (zval * aval, char * key, long lval); add_assoc_double (zval * aval, char * key, double dval); add_assoc_string (zval * aval, char * key, char * strval, int dup ); add_assoc_stringl (zval * aval, char * key, char * strval, uint strlen, int dup); add_assoc_zval (zval * aval, char * key, zval * value); // Note: in fact, these functions are macros and are encapsulated by add_assoc _ * _ ex functions. // Add_index _ * series functions: ZEND_API int add_index_long (zval * arg, ulong idx, long n); ZEND_API int add_index_null (zval * arg, ulong idx ); ZEND_API int add_index_bool (zval * arg, ulong idx, int B); ZEND_API int add_index_resource (zval * arg, ulong idx, int r); ZEND_API int add_index_double (zval * arg, ulong idx, double d); ZEND_API int add_index_string (zval * arg, ulong idx, const char * str, int duplicate); ZEND_API int add_index_stringl (zval * arg, ulong idx, const char * str, uint length, int duplicate); ZEND_API int add_index_zval (zval * arg, ulong index, zval * value); // add_next_index _ * function: ZEND_API int add_next_index_long (zval * arg, long n); ZEND_API int add_next_index_null (zval * arg); ZEND_API int add_next_index_bool (zval * arg, int B ); ZEND_API int add_next_index_resource (zval * arg, int r); ZEND_API int add_next_index_double (zval * arg, double d); ZEND_API int add_next_index_string (zval * arg, const char * str, int duplicate); ZEND_API int add_next_index_stringl (zval * arg, const char * str, uint length, int duplicate); ZEND_API int add_next_index_zval (zval * arg, zval * value );

    Next we can compare the macros corresponding to common Array Operations.

    Add_next_index _*()

    $ Arr [] = NULL; add_next_index_null (arr); $ arr [] = 42; add_next_index_long (arr, 42); $ arr [] = true; add_next_index_bool (arr, 1); $ arr [] = 3.14; add_next_index_double (arr, 3.14); $ arr [] = 'foo'; add_next_index_string (arr, "foo "); $ arr [] = $ var; add_next_index_zval (arr, zval );

    Add_index _*()

    $ Arr [0] = NULL in the PHP kernel; add_index_null (arr, 0); $ arr [1] = 42; add_index_long (arr, 1, 42 ); $ arr [2] = true; add_index_bool (arr, 2, 1); $ arr [3] = 3.14; add_index_double (arr, 3, 3.14 ); $ arr [4] = 'foo'; add_index_string (arr, 4, "foo", 1); $ arr [5] = $ var; add_index_zval (arr, 5, zval );

    Add_assoc _*()

    $ Arr ["abc"] = NULL; add_assoc_null (arr, "abc"); $ arr ["def"] = 42; add_assoc_long (arr, "def", 42); $ arr ["ghi"] = true; add_assoc_bool (arr, "ghi", 1 ); $ arr ["jkl"] = 3.14add _ assoc_double (arr, "jkl", 3.14); $ arr ["mno"] = "foo" add_assoc_string (arr, "mno ", "foo", 1 "); $ arr [" pqr "] = $ var; add_assoc_zval (arr," pqr ", zval );

    A complete instance

    The following defines a function in PHP and uses arrays in it. Then let's see how to implement it in the kernel.


       

    In the kernel:

    PHP_FUNCTION(array_test){char* mystr;zval* mysubarray;array_init(return_value);add_index_long(return_value, 42, 123);add_next_index_string(return_value, "test", 1);add_next_index_stringl(return_value, "test_stringl", 10, 1);mystr = estrdup("Forty Five");add_next_index_string(return_value, mystr, 0);add_assoc_double(return_value, "double", 3.14);ALLOC_INIT_ZVAL(mysubarray);array_init(mysubarray);add_next_index_string(mysubarray, "hello", 1);add_assoc_zval(return_value, "subarray", mysubarray);}

    You may wonder where the variable return_value in the above Code is defined. Next, expand PHP_FUNCTION and you will understand it.

    zif_array_test(int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC);

    Yes. In fact, each function has a default return value return_value. When RETVAL _ * () and RETURN _ * () are used as function RETURN values, only return_value is modified.

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.