PHP kernel-static variable, constant, magic constant principle of the specific introduction

Source: Internet
Author: User
Tags define function php source code

This article through the PHP source code, starting from the structure of static variables, constants, magic constants to analyze.

1. Static variables

As we all know, static variables are loaded when the PHP script loads, That is 1. The object can be called directly without new, 2. and static variables are stored in common areas with multiple objects of the same class to work with a static variable, 3. Static variables only after the script end memory will be released, for these three questions, want to ask, why?

Expand the narrative below

First look at its structure, to better analyze and understand.

Static variables are stored in the function structure body _zend_execute_data,

In this structure, there are two key structures, Op_array and symbol_table .

1.*symbol_table store the various variables inside this class, each new object, will open up the environment space, see the PHP kernel--On the PHP soul hashtble case Two,

2. function compiled opcode stored in *op_array; structure, which is the logic of this function, each time the new object, the public one space logic, will not be in the independent open up the environment space "is critical to achieve static root cause"

Zend/zend_compiles.h 384 line, execution environment structure

struct _zend_execute_data {struct _zend_op *opline;zend_function_state function_state;zend_op_array *op_array;//!!!!! After the function compiles the execution logic, compiles the opcode binary code, is called the Op_array, the common one logic zval *object; HashTable *symbol_table;//!!!!! The symbolic table address of this function, each time new opens a space for "---------struct _zend_execute_data *prev_execute_data;zval *old_error_reporting;zend_ BOOL Nested;zval **original_return_value;zend_class_entry *current_scope;zend_class_entry *current_called_scope; Zval *current_this;  struct _ZEND_OP *fast_ret; /* Used by Fast_call/fast_ret (finally keyword) */call_slot *call_slots;call_slot *call;};

Zend/zend_compiles.h 261 lines, Op_array structure code

struct _zend_op_array {/* Common elements */zend_uchar type;.../* static variables support */hashtable *static_variables; 294 rows, static variables ... }

Example:

T1 () {$a +=1; static $b +=1;t1 (); T1 ();}//Add itself to call 3 times

The result is $1 per return, and $b =

The reasons are as follows:

Three Call function open symbol table "3 parts "

[T_3 Execute_data]---->[symbol_table_3]

[t_2 Execute_data]---->[symbol_table_2]

[t_1 Execute_data]---->[symbol_table_1]

*op_array->* static Variable table "one copy "


Conclusion :

The variables of the class are stored in *symbol_table, each with its scope, and each instantiation opens up an environment space (see Hashtable for an example of the second part), while the static variable, as shown in the code, is stored in Op_array, what Op_array is , compiling the generated opcode code, storing the logic of the function, regardless of the number of new objects, this logic is common, and static variables are stored in the structure, so the implementation of the same class of different objects can be common a static variable, also explained at the PHP level, Why static variables are called directly without new. Explained the question one or two,

Because static variables are stored in Op_array, Op_array is released at the end of the script execution, so it is also released at this time., explaining the problem three.

2. Constants


The first is to see the difference between constants and variables, which adds an extra element based on the ZVAL structure of the variable. The internal structure of the constants in PHP is shown below.

The structure of the constant (33 lines of the Zend/zend_constants.h file)

typedef struct _ZEND_CONSTANT {    zval value;/* zval structure, PHP internal variable storage structure */    char *name;/* Constant name */    UINT NAME_LEN;
  int flags;  /* tags for constants such as Const_persistent | Const_cs */    int module_number;  /* Module number */} zend_constant;

Structure as above, Name,name_len at a glance, it is worth mentioning that zval and variables stored in the ZVAL structure is identical, (see the PHP kernel storage mechanism (separation/change))

The main explanation is flag and module_number

1.flags:
C.flags = Case_sensitive/case insensitive; 1,0
Assign to struct field is case sensitive

2.module_number:
1.php_user_constant: User-defined constants
(The module number for constants defined by the Define function is all)
2.register_main_long_constant:php built-in definition constants
such as error reporting level E_all, e_warning,php_version and other constants, are persistent constants, and finally destroyed

3. Magic Constants

say is a constant, in fact each time the value in different position, may be not the same, the reason is why?

Instead of parsing at run time, the PHP kernel replaces the content assignments of these constants in lexical parsing. The following PHP code:


In __function__, for example, in the Zend/zend_language_scanner.l file, __function__ is a meta tag (token) that needs to be parsed:


Right here, when the current middle code is in a function, the current function name is assigned to Zendlval (that is, the value content of token T_func_c), and if not, an empty string is assigned to Zendlval (so print directly in the top-level role domain name __function_ _ will output spaces). This value is directly assigned to the return value when parsing. So we can see in the generated intermediate code that the positions of these constants have been assigned values.

Just keep in mind that the functionality of the above code is translated into the corresponding value at the time of lexical analysis, __function__.

(There are CG and eg two macros in PHP, respectively, to obtain Compile_global data and excutor_global data, respectively, they have their own function_table and class_table,
In addition, PHP require is performed as a function, so this time you need to know how eg and CG are converted. )

Related Article

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.