PHP kernel research static variable _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP kernel studies static variables. Static variables can be static global variables. If unset is not called, this static variable will exist until the program exits and is managed by Zend memory to release it. it can be static local static variables.

It can be a static global variable. If unset is not called, the static variable will exist until the program exits and is managed by the Zend memory.

It can be a static local variable: defined in the function. after the function is executed, the static variable will not disappear.

It can be a static member variable: defined in the class, which can be shared among all class objects.

For example

Function test (){

Static $ a = 1;

$ A ++;

}

Test (); // $ a = 2

Test (); // $ a = 3

Test (); // $ a = 4

Last $ a = 4 ..

Next we will analyze it from the kernel

Static is not a function, it is a keyword, so it can only be searched from LEX syntax analysis.

Open zend/zend_language_scanner.l to find static

Find code

"Static "{

Return T_STATIC;

}

Is a macro

Then open zend/zend_language_pareser.l

Search T_STATIC

Find code

T_STATIC static_var_list ';'

Follow up static_var_list

Find code

Static_var_list:

Static_var_list ', 't_variable {zend_do_fetch_static_variable (& $3, NULL, ZEND_FETCH_STATIC TSRMLS_CC );}

| Static_var_list ', 't_variable' = 'static_scalar {zend_do_fetch_static_variable (& $3, & $5, ZEND_FETCH_STATIC TSRMLS_CC );}

| T_VARIABLE {zend_do_fetch_static_variable (& $1, NULL, ZEND_FETCH_STATIC TSRMLS_CC );}

| T_VARIABLE '= 'static_scalar {zend_do_fetch_static_variable (& $1, & $3, ZEND_FETCH_STATIC TSRMLS_CC );}

;

It is resolved from the PHP syntax analysis program to the above code.

Zend_do_fetch_static_variable.

It is defined in zend/zend_compile.c

The code is as follows:

Void zend_do_fetch_static_variable (znode * varname, const znode * static_assignment, int fetch_type TSRMLS_DC)

{

Zval * tmp; // A temporary variable

Zend_op * opline;

Znode lval;

Znode result;

ALLOC_ZVAL (tmp); // apply for a piece of memory

If (static_assignment ){

* Tmp = static_assignment-> u. constant;

} Else {

INIT_ZVAL (* tmp );

}

If (! CG (active_op_array)-> static_variables) {// initialize the HASH key value of the static variable

ALLOC_HASHTABLE (CG (active_op_array)-> static_variables );

// Initialize the HASH value

Zend_hash_init (CG (active_op_array)-> static_variables, 2, NULL, ZVAL_PTR_DTOR, 0 );

}

Zend_hash_update (CG (active_op_array)-> static_variables, varname-> u. constant. value. str. val, varname-> u. constant. value. str. len + 1, & tmp, sizeof (zval *), NULL );

If (varname-> op_type = IS_CONST ){

If (Z_TYPE (varname-> u. constant )! = IS_STRING ){

Convert_to_string (& varname-> u. constant );

}

}

Opline = get_next_op (CG (active_op_array) TSRMLS_CC );

Opline-> result. op_type = IS_VAR;

Opline-> result. u. EA. type = 0;

Opline-> result. u. var = get_temporary_variable (CG (active_op_array ));

Opline-> op1 = * varname;

SET_UNUSED (opline-> op2 );

Opline-> op2.u. EA. type = ZEND_FETCH_STATIC;

Result = opline-> result;

If (varname-> op_type = IS_CONST ){

Zval_copy_ctor (& varname-> u. constant );

}

Fetch_simple_variable (& lval, varname, 0 TSRMLS_CC);/* Relies on the fact that the default fetch is BP_VAR_W */www.2cto.com

If (fetch_type = ZEND_FETCH_LEXICAL ){

Znode dummy;

Zend_do_begin_variable_parse (TSRMLS_C );

Zend_do_assign (& dummy, & lval, & result TSRMLS_CC );

Zend_do_free (& dummy TSRMLS_CC );

} Else {

Zend_do_assign_ref (NULL, & lval, & result TSRMLS_CC );

}

CG (active_op_array)-> opcodes [CG (active_op_array)-> last-1]. result. u. EA. type | = EXT_TYPE_UNUSED;

/* Zval_dtor (& varname-> u. constant );*/

}

Else it can be a static global variable. If unset is not called, this static variable will always exist until the program exits and is managed by the Zend memory to release it. it can be a static local variable...

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.