If static is a static variable in PHP, and he can define a function, and the variable is a global static variable, then how do we add static to the function or variable that will affect the functions and variables, let's look at it together.
1) The description of the global variable (the external variable) is then prefixed with static, which constitutes the global variable. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the entire source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.
2) from the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.
3) The static function differs from the normal function scope only in this file. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to which the function is to be used is to contain the header file
After PHP5.3.0, we can use a variable to invoke the class dynamically. However, the value of the variable cannot be the keyword self, parent, or static.
Example #1 static Member code example
<?phpclass 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 ();p rint $foo->staticvalue (). " ";p rint $foo->my_static." "; Undefined "Property" My_staticprint $foo:: $my _static. ""; $classname = ' Foo ';p rint $classname:: $my _static. " "; The print Bar can be dynamically called after PHP 5.3.0:: $my _static. ""; $bar = new bar ();p rint $bar->foostatic (). " ";? >
Example #2 static Method code example
<?phpclass Foo {public static function Astaticmethod () { //... }} Foo::astaticmethod (); $classname = ' Foo '; $classname:: Astaticmethod (); As of PHP 5.3.0?>
With regard to the use of the static keyword in the class, the PHP manual gives the following conventions:
1, declaring a class member or method is static, you can not instantiate the class and directly access. Static members cannot be accessed through an object (except static methods).
2, because the static method does not need to be called through the object, so the pseudo-variable $this in the static method is not available.
3. Static properties cannot be accessed by the object by operator.
4, using:: The way to call a non-static method will cause a e_strict level error.
Now take a look at the 4th Convention.
Operating environment: (WIN32) php/5.3.3
Class foo{public static $my _static = ' Foo ';//declares 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:: method to invoke a non-static method } } echo Foo:: $my _ Static. "<br >"; Echo Foo::run ();//ClassName:: Method name Call non-static method echo Foo::callrun ();
Static keyword effect:
The use of static variables in PHP is wider, and we can add the static modifier to a class, method, or variable before we even add the static keyword to the function's internal variables. A variable with the static modifier added is not lost even after the function has finished executing, that is, the variable remembers the original value the next time the function is called.