PHPstatic static variables and attributes

Source: Internet
Author: User
Variables are clearly different from other variables. next I will introduce you to static variables and attribute methods, static variable references, and static function usage. For more information, see. static variables: Another important feature of variable range is static variables (stat... variables are clearly different from other variables. next I will introduce you to static variables and attribute methods, static variable references, and static function usage. For more information, see.

Static variables: Another important feature of the variable range is static variable. static variables only exist in local function domains. However, when the program runs out of this scope, their values are not lost, let's take a look at the example below.

Example 7-4. demonstrate the example that requires static variables. the code is as follows:

 

This function is useless, because every call sets the value of $ a to 0 and outputs "0". adding $ a ++ to the variable does not work, because once you exit this function, the variable $ a does not exist. to write a counting function that does not lose the current count value, you need to define the variable $ a as static.

Example 7-5. the code for using static variables is as follows:

 

Now, every time you call the Test () function, the $ a value is output and the value is added.

Static variables also provide a method for processing recursive functions. recursive functions are called to call their own functions. be careful when writing recursive functions, because they may be infinitely recursive, make sure there are sufficient methods to stop recursion. The simple function recursively counts to 10 and uses the static variable $ count to determine when to stop.

For example 7-6, static variables and recursive functions, the code is as follows:

 

Note: static variables can be declared according to the preceding example. if you assign a value to the expression result in the declaration, parsing errors will occur.

Example 7-7. declare static variables with the following code:

 

Reference of global and static variables

In the first generation of the Zend Engine, PHP4 is driven. the static and global definitions of variables are implemented in the form of references. for example, A real global variable imported using a global statement in a function domain is actually a reference to a global variable, which may lead to unexpected behavior, as demonstrated in the following example, the code is as follows:

 

Executing the preceding example will result in the following output:

NULL object(stdClass)(0) { }

Similar behavior applies to static statements. References are not stored statically. the code is as follows:

 Property ++; return $ obj;} function & get_instance_noref () {static $ obj; echo "Static object:"; var_dump ($ obj); if (! Isset ($ obj) {// assign an object to the static variable $ obj = new stdclass;} $ obj-> property ++; return $ obj ;} $ obj1 = get_instance_ref (); $ still_obj1 = get_instance_ref (); echo "n"; $ obj2 = response (); $ still_obj2 = get_instance_noref ();?>

Executing the preceding example will result in the following output:

Static object: NULL Static object: NULL  Static object: NULL Static object: object(stdClass)(1) {   ["property"]=>   int(1) }

The preceding example demonstrates that when a reference is assigned to a static variable, the value of the second call to the & get_instance_ref () function is not remembered.

Note:

1. static variables declared outside the function have little significance. static variables declared inside the function are limited by the scope, and the internal static variables cannot be modified outside the function.

2. the referenced variable is also a variable, but its value is the memory address of the variable.

The reserved words global and static in php are as follows:

 

There are variables $ I both inside and outside the function, but the two of them are completely different variables. The $ I outside the function is a global variable, the memory space will not be released until the script stops running. $ I in the function is a local variable. when the program stream passes through the function, it is initialized and exited, the memory is recycled by the system. when the function is called again, the memory space is allocated again and the memory space is recycled. the memory space allocated again may be the same memory address or the same memory address.

Unlike $ I, $ j converts a local variable to a global variable by using the global keyword. when the global_var () function is called, $ j is not allocated memory space again, similarly, $ B can be printed out of the function, but $ c cannot be printed because $ B is a global variable and will not be destroyed, while $ c cannot be printed. $ c does not exist anymore, the code is as follows:

 

First, we can see $ B and $ c outside the function, that is, global variables and static variables. static modification does not make much sense here, because they are all stored in the data segment (data-segment ), it will not be recycled until the script is run out. then, let's look at $ I and $ c in the function. after the function is called, $ I and $ c are not actually recycled, however, the $ I output is NULL and the $ c output is 3, because their scope is inside the function, not outside the function, that is, $ I and $ c are invisible outside the function, the significance of the static variable in the function lies in this. it is visible only inside the function and will not be destroyed. that is to say, the variable will not be recycled but not modified by other functions when the function exits. (Note: The $ c variable outside the function and in the function is two different variables)

The code is as follows:

 

In the above example, $ j is always 1, and $ I accumulates 1 every time it is called. this is because local variables are stored in the heap segment and will be recycled every time you exit the function, while $ I is stored in the data segment (data-segment), it will not be recycled until the program is executed. static variables, which we usually call, if not specified, all of them are static variables in the function.

Reference functions and static variables

Since static variables will not be destroyed until the execution of the script ends, is there any way to access the value of the variable? Let's take a look at the following example. the code is as follows:

 

The output values of the two question marks are 8 and 12, respectively, indicating that the variables can still be accessed as long as they are not destroyed, we can return the address of the static variable to other functions by referencing the function. other functions can access and modify its value through the address of the static variable.

First in the above example ??, Why 8 instead of 9? this is because the what_ I ($ ptr) function requires that the parameter be passed by value, that is, the $ ptr real parameter value here is 5, the $ ptr parameter and the global variable $ ptr are two different variables. Second place ?? The value is 12. why is it not 11. What_p (& $ ptr) function, requires that the parameter is passed by reference, that is, $ ptr here is the address pointing to the static variable $ I, note that the $ ptr parameter and the global variable $ ptr are two different variables, but they all point to the same place.


Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.