PHP Variable Storage structure

Source: Internet
Author: User
Tags variable scope vars

First of all, I did not read PHP source, just for PHP sometimes strange performance of interest, find a developer Laruence blog combined with PHP provided by the function of Debug_zval_dump spying has been described in this blog work mechanism. If you want to have an understanding of the PHP variable storage structure or want to deepen the understanding of PHP variables, this article is suitable for you, more in-depth to see the source code bar.

To ensure the consistency of the blog, first refer to laruence about the internal storage structure of PHP variables (slightly modified)

In PHP, all variables are stored in a structure-zval, and in zend/zend.h we can see the definition of Zval:

    1. typedef struct _zval_struct {
    2. zvalue_value value;
    3. zend_uint refcount;
    4. Zend_uchar type;
    5. Zend_uchar is_ref;
    6. } zval;

Where Zvalue_value is a key part of truly preserving data, defined as a Union (union)

  1. typedef Union _zvalue_value {
  2. long lval;
  3. double dval;
  4. struct {
  5. Char *val;
  6. int len;
  7. } str;
  8. HashTable *ht;
  9. zend_object_value obj;
  10. } zvalue_value;

The types of variables that are common in PHP are:

    1. 1. Integer/floating-point/Long integer/bool value, etc.
    2. 2. String
    3. 3. Array/associative array
    4. 4. Objects
    5. 5. Resources

PHP stores the true type of a variable based on the Type field in Zval, and then chooses how to get the value of the Zvalue_value based on the type, for example, Integer and bool values:

    1. Zval.type = is_long;//Shaping
    2. Zval.type = is_bool;//Boolean value

Go to fetch zval.value.lval, for bool value lval∈ (0|1), if is double precision, or float will take zval.value dval. And if it's a string, then:

    1. Zval.type = is_string

This time, will take: Zval.value.str and this is also a structure, the string containing C-cell and the length of the string.

For arrays and objects, the type corresponds to Is_array, Is_object, respectively, zval.value.ht and obj.

In particular, resources, in PHP, resources are a very special variable, any variables that are not part of PHP's built-in variable types are considered as resources to save, such as database handles, open file handles, and so on. For resources:

    1. Type = Is_resource

At this time, will go to fetch zval.value.lval, at this time the lval is an integer indicator, and then PHP will be based on this indicator in PHP built in a resource list to query the corresponding resources, at this time the lval as if it corresponds to the resource linked list of the offset value.

    1. zend_fetch_resource(Con, type, Zval *, default, Resource_name, Resource_type) ;

With this mechanism, PHP implements a weak type, because for ze, it always faces the same type, that is zval.

The above section of the blog just clarified the internal representation of the PHP variable, to know how the internal representation and the variables in the user script are linked, need to see laruence another blog post in-depth understanding of the PHP principle variable scope (scope in PHP), the same reference part of the content (slightly modified) as follows:

If you write in the script:

    1. <?php
    2. $var = "Laruence";
    3. echo $var;
    4. ?>

How does ze relate my var and internal structure Zval?

PHP internally uses Zval to represent variables, but for the above script, our variables are named, var. In Zval, there is no corresponding field to reflect the variable name. There must be a mechanism inside PHP that implements the mapping of variable names to Zval.

In PHP, all variables are stored in an array (the exact hash table).

When you create a variable, PHP assigns a zval to the variable, fills in the corresponding variable value, and then fills in an array with the name of the variable and the pointer to the Zval. Then, when you get this variable, PHP will get the corresponding zval by looking up the array.

View the _zend_executor_globals structure (this structure saves some execution-related contextual information in the PHP actuator)

  1. struct _zend_executor_globals {
  2. ....
  3. HashTable *active_symbol_table; /* Activity Symbol Table * /
  4. HashTable symbol_table; / * Global symbol Table * /
  5. HashTable included_files;
  6. jmp_buf *bailout;
  7. int error_reporting;
  8. .....
  9. }

Summarize the above two blogs for the following programs

[PHP]View Plaincopyprint?
  1. <?php
  2. class Class1
  3. {
  4. public $member;
  5. function __construct ()
  6. {
  7. $this->member = 1;
  8. }
  9. }
  10. $a = 1;
  11. $b = 0.56;
  12. $c = "string";
  13. $d = Array (1=>1);
  14. $e = new Class1;
  15. $f = Tmpfile ();
  16. Debug_zval_dump ($a);
  17. Debug_zval_dump ($b);
  18. Debug_zval_dump ($c);
  19. Debug_zval_dump ($d);
  20. Debug_zval_dump ($e);
  21. Debug_zval_dump ($f);
  22. ?>

The program uses Debug_zval_dump to spy on Long, DOUBLE, STRING, ARRAY, OBJECT, resource type variables, and the result is as follows

[Plain]View Plaincopyprint?
    1. Long (1) refcount (2)
    2. Double (0.56) RefCount (2)
    3. String (6) "String" RefCount (2)
    4. Array (1) refcount (2) {
    5. [1]=>
    6. Long (1) refcount (1)
    7. }
    8. Object (Class1) #1 (1) refcount (2) {
    9. ["Member"]=>
    10. Long (1) refcount (1)
    11. }
    12. Resource (4) of type (stream) RefCount (2)

The analysis plots the entire storage structure as follows

With this diagram, you can see how PHP's various types of variables are stored in memory and how user variables are linked to the memory structure, which is the basis of the next blog post about PHP references. Look forward to the next PHP assignment behavior in detail.

PHP Variable Storage structure

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.