Influence of static keywords on variables and functions in php

Source: Internet
Author: User

In php, static is a static variable. It can define a function. The variable is a global static variable, so how does adding static before a function or variable affect the function and variable? Let's take a look at it.

 
1) The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

2) from the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.

3) static functions and common functions have different scopes. They are only available in this file. Only functions used in the current source file should be declared as internal functions (static). Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file.


After PHP5.3.0, we can use a variable to dynamically call the class. However, the value of this variable cannot be self, parent, or static.

Example #1 static member code Example

The Code is as follows: Copy code


<? Php
Class Foo
{
Public static $ my_static = 'foo ';

Public function staticValue (){
Return self: $ my_static;
}
}

Class Bar extends Foo
{
Public function fooStatic (){
Return parent: $ my_static;
}
}


Print Foo: $ my_static ."";

$ Foo = new Foo ();
Print $ foo-> staticValue ()."";
Print $ foo-> my_static. ""; // Undefined "Property" my_static

Print $ foo: $ my_static ."";
$ Classname = 'foo ';
Print $ classname: $ my_static. "; // can be called dynamically after PHP 5.3.0

Print Bar: $ my_static ."";
$ Bar = new Bar ();
Print $ bar-> fooStatic ()."";
?>

Example #2 static method code Example

<? Php
Class Foo {
Public static function aStaticMethod (){
//...
}
}

Foo: aStaticMethod ();
$ Classname = 'foo ';
$ Classname: aStaticMethod (); // As of PHP 5.3.0
?>

The PHP Manual provides the following conventions about the use of Static keywords in a class:


1. If the declared class member or method is static, you can directly access it without instantiating the class. You cannot access static members through an object (except for static methods ).

2. Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.
3. Static attributes cannot be accessed by objects through the-> operator.
4. Use: to call a non-static method may cause an E_STRICT error.
Now let's take a look at the 4th conventions.
Running environment: (Win32) PHP/5.3.3

The Code is as follows: Copy code

Class Foo {

Public static $ my_static = 'foo'; // declare a static member


Public function staticValue () {// static method
Return self: $ my_static ;//
}
Public function run () {// non-static method
Return "abc <br> ";
}
Public function callrun (){
Return self: run (); // use self: to call a non-static method

}

}

Echo Foo: $ my_static. "<br> ";

Echo Foo: run (); // use className: method name to call a non-static method
Echo Foo: callrun ();

Static keywords for interview questions


Question code: Write the calculation result of the following code ()

The Code is as follows: Copy code

<? Php

Function dewen (){
$ K = add_number (100 );
$ K + = add_number (100 );
Printf ("% d", $ k );
Return 0;
}

Function add_number ($ n ){
Static $ I = 100;
$ I + = $ n;
Return $ I;
}

At first glance, we thought it was a simple self-increment calculation: We thought the result was 400, and the answer to the result was incorrect. Finally, I carefully read the code and learned that the code is 500. $ I + = $ n is printed twice. Before calculation, $ I is 100 at a time and 200 at a time. It is known that the problem is static. Then Baidu suddenly realized the static keyword.

Static Keyword:

PHP uses static variables in a wider range. We can not only add static modifiers before classes, methods, or variables, but also add static keywords to internal variables of the function. The variable with the static modifier is not lost even after the function is executed. That is to say, the variable still remembers the original value when the function is called the next time. For example:

The Code is as follows: Copy code

01 <? Php

02 function test ()

03 {

04 static $ var1 = 1;

05 $ var1 + = 2;

06 echo $ var1 .'';

07}

08

09 test ();

10 test ();

11 test ();

12?>
The running result is as follows:

3 5 7

To sum up:

What is the difference between static global variables and common global variables:
Static global variables are modified only once to prevent being referenced in other file units;
What is the difference between static local variables and common local variables:
Static local variables are initialized only once, and the next time is based on the previous result value;
What are the differences between static functions and common functions:
The static function has only one copy in the memory, and the normal function maintains one copy in each call.

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.