PHP 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, reference static variables, and usage of static functions. For more information, see.

Static variables

Another important feature of variable range is static variable ). Static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. Take a look at the following example:

Example 7-4. Example of static variables

The code is as follows: Copy code
<? Php
Function Test ()
{
$ A = 0;
Echo $;
$ A ++;
}
?>
 

This function is useless, because every call will set the value of $ a to 0 and output "0 ". Adding $ a ++ to the variable does not work, because $ a does not exist once you exit this function. To write a counting function that does not lose the current count value, you must define variable $ a as static:

Example 7-5. Example of using static variables

The code is as follows: Copy code
<? Php
Function Test ()
{
Static $ a = 0;
Echo $;
$ A ++;
}
?>

 

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

Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because infinite recursion may occur. Make sure there are sufficient methods to abort recursion. The simple function calculates 10 recursively and uses the static variable $ count to determine when to stop:

Example 7-6. Static variables and recursive functions

The code is as follows: Copy code

<? Php
Function Test ()
{
Static $ count = 0;

$ Count ++;
Echo $ count;
If ($ count <10 ){
Test ();
}
$ Count --;
}
?>

 
 

Note: static variables can be declared according to the preceding example. If a value is assigned to the expression result in the declaration, the parsing error occurs.

Example 7-7. Declare static variables

The code is as follows: Copy code

<? Php
Function foo (){
Static $ int = 0; // correct
Static $ int = 1 + 2; // wrong (as it is an expression)
Static $ int = sqrt (121); // wrong (as it is an expression too)

$ Int ++;
Echo $ int;
}
?>

 
 

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 within a function domain is actually a reference to a global variable. This may lead to unexpected behaviors, as demonstrated in the following example:

The code is as follows: Copy code


<? Php
Function test_global_ref (){
Global $ obj;
$ Obj = & new stdclass;
}

Function test_global_noref (){
Global $ obj;
$ Obj = new stdclass;
}

Test_global_ref ();
Var_dump ($ obj );
Test_global_noref ();
Var_dump ($ obj );
?>
 

Executing the preceding example will result in the following output:

The code is as follows: Copy code

NULL
Object (stdClass) (0 ){
}

Similar behavior applies to static statements. References are not stored statically:

 

The code is as follows: Copy code

<? Php
Function & get_instance_ref (){
Static $ obj;

Echo "Static object :";
Var_dump ($ obj );
If (! Isset ($ obj )){
// Assign a reference value to a static variable
$ Obj = & new stdclass;
}
$ Obj-> property ++;
Return $ obj;
}

Function & get_instance_noref (){
Static $ obj;

Echo "Static object :";
Var_dump ($ obj );
If (! Isset ($ obj )){
// Assign an object to a static variable
$ Obj = new stdclass;
}
$ Obj-> property ++;
Return $ obj;
}

$ Obj1 = get_instance_ref ();
$ Still_obj1 = get_instance_ref ();
Echo "n ";
$ Obj2 = get_instance_noref ();
$ Still_obj2 = get_instance_noref ();
?>

 

Executing the preceding example will result in the following output:

The code is as follows: Copy code

Static object: NULL
Static object: NULL

Static object: NULL
Static object: object (stdClass) (1 ){
["Property"] =>
Int (1)
}

The preceding example shows 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 are of little significance. The static variables declared inside the function are limited by the scope. The static variables inside the function cannot be modified outside the function.
2. The referenced variable is also a variable, but its value is the memory address of the variable.

Php reserved words global and static

The code is as follows: Copy code
<? Php
$ I = $ j = 8;
Function global_var (){
Global $ j, $ B;
$ C = $ j = $ B = $ I = 4;
}
Global_var ();
Echo "I: $ I, j: $ j B: $ B c: $ c n"; // I: 8, j: 4 B: 4 c:
?>

$ I is included in and outside the function, but the two of them are completely different variables. $ I outside the function is a global variable, and 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. When the function is exited, the memory is recycled by the system and the function is called again, the memory space is allocated again and the memory space is recycled. The allocated memory space may be the same memory address or the same memory address.
Unlike $ I, $ j converts the keyword global to "global variable. When the global_var () function is called, the memory space of $ j is not allocated 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. $ C cannot be printed, and $ c does not exist anymore. After exiting the function, it is destroyed.

The code is as follows: Copy code
<? Php
$ A = 2;
Static $ B = 2;
Static $ c = 3;
Function global_var (){
Static $ I, $ c;
Global $ j, $;
$ C = $ a = $ B = $ I = $ j = 4;
}
Global_var ();
Echo "a: $ a B: $ B c: $ c I: $ I j: $ j"; // a: 4 B: 2 c: 3 I: j: 4
?>

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) and 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 a function lies in this: it is visible only inside the function and will not be destroyed. That is to say, the warranty letter exits the function, and the variable will not be recycled, but will not be modified by other functions. (Note: The $ c variable outside the function and in the function is two different variables)

The code is as follows: Copy code
<? Php
Function global_var (){
Static $ I;
+ + $ J;
++ $ I;
Echo "j: $ j I: $ I n ";
}
Global_var (); // j: 1 I: 1
Global_var (); // j: 1 I: 2
Global_var (); // j: 1 I: 3
?>

In the preceding 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. $ I is stored in the data segment (data-segment) and will not be recycled until the program is executed. Static variables we usually call are static variables in the function unless otherwise specified.

Reference functions and static variables

Static variables will not be destroyed until the execution of the script ends. Is there a way to access the value of this variable? Let's take a look at the following example:

The code is as follows: Copy code
<? Php
Get_local_static_var ();
$ Ptr = & get_local_static_var ();
Get_local_static_var ();
+ + $ Ptr;
Get_local_static_var ();
What_ I ($ ptr );
Get_local_static_var ();//??
What_p ($ ptr );
Get_local_static_var ();//??
 
Function & get_local_static_var (){
Static $ I;
++ $ I;
Echo "I: $ I n ";
Return $ I;
}
 
Function what_ I ($ ptr ){
$ I = & get_local_static_var ();
++ $ I;
+ + $ Ptr;
}
 
Function what_p (& $ ptr ){
$ I = & get_local_static_var ();
++ $ I;
+ + $ Ptr;
}
?>

The two question marks are output at 8 and 12 respectively. This shows that variables can still be accessed as long as they are not destroyed. We can return the static variable address to other functions by referencing the function. Other functions can access and modify the value of static variables.
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.

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.