php basic grammar underlying implementation

A static variable:
Features: Static variables return of function calls and does not release its results will be retained until the next call to the function, its life cycle is longer than local variable;

Static variables can be divided into:

  • Static global variables, global variables in PHP can also be understood as a static global variable, because unless explicitly unset release, there is always the program is running.
  • Static local variable, namely the static variables defined within a function, function when performing operations on variables will remain until the next function is called.
  • Static member variables, which should be relatively static variables defined in the class, and instance variables, static member variables can be shared among all instances.

The underlying logic: Static variables are not the same as ordinary variables allocated on zend_execute_data, but stored in zend_op_array-> sratic_variables, which is a hash structure. Static variable is initialized only once, and his initialization occurs at compile time instead of the implementation phase ; the following example

function my_func(){

statis $count = 5;

$count++;

echo $count;

}

my_func();

my_func();

Final output: 6,7

The above example, found at compile time define a static variable, then into zend_op_array-> sratic_variables, will not be re-defined at compile time, there is no reset value, because the average variable is defined in the implementation phase, the initial static variables not be a variable value ;

Second, the constant

Features: You can not change during the execution of the script

EG constants are stored in the hash table, the visit is also based on the constant name directly into the hash table lookup,

Other differences define and const of: 
1.const no longer a conditional statement defined constants, but can define the 
2.const adopt a common constant name, define the expression can be used as the name of 
3.const only accept a static scalar, and define any expression can be used. 
4.const itself is a language structure. And define a function. So use const speed much faster.
Third, global variables

Features: definition of variables in addition to the function, class becomes a global variable;

Values of global variables are stored in the global symbol table, the global variable is not registered to the symbol table by EG individual instruction, but before the execution , with a core main code automatically variables into local variables global symbol table, even if the code where there are no global variables will be imported into the symbol table process, fall in import zend_execete_ec () completed prior to execution. Access global variables are global variables will be converted to a reference type, then the local variable to point to this reference,

Step: zend_attach_symbol_table () the local variables into the global symbol table, there will be a local variable names variable zend_op_array-> vars array, and then the variable named key inserted EG, value of a corresponding point on the local variables zend_execute_data.

Guess you like

Origin blog.csdn.net/qq_38234594/article/details/89416126