The impact of the static keyword in PHP on variables and functions_PHP Tutorial

WBOY
Release: 2016-07-13 10:49:35
Original
1059 people have browsed it

In php, static is a static variable. It can define functions. The variables are global static variables. So what impact will adding static in front of the function or variable have on the function and the variable? Let’s do it together. have a look.


1) The description of global variables (external variables) is preceded by static to constitute a static global variable. Global variables themselves are static storage methods, and static global variables are of course also static storage methods. There is no difference between the two in the way they are stored. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. Static global variables limit their scope, that is, they are only valid within the source file in which the variable is defined, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be shared by functions in that source file, errors can be avoided in other source files.

2) From the above analysis, it can be seen that changing a local variable to a static variable changes its storage method and changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use. ​

3) The scope of static functions is different from ordinary functions, only in this file. Functions used only in the current source file should be declared as internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, they should be stated in a header file. The source files that use these functions must include this header file


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

Example #1 Static member code example

The code is as follows Copy code
 代码如下 复制代码


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 . " "; // PHP 5.3.0之后可以动态调用

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

 Example #2 静态方法代码示例

class Foo {
public static function aStaticMethod() {
// ...
}
}

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

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 class Foo {<🎜> Public static function aStaticMethod() {<🎜>                // ...<🎜> }<🎜> }<🎜> <🎜>Foo::aStaticMethod();<🎜> $classname = 'Foo';<🎜> $classname::aStaticMethod(); // As of PHP 5.3.0<🎜> ?>

Regarding the use of the Static keyword in classes, the PHP manual gives the following convention:


1. Declare class members or methods as static so that they can be accessed directly without instantiating the class. Static members (except static methods) cannot be accessed through an object.

2. Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods.
3. Static properties cannot be accessed by objects through the -> operator.
4. Calling a non-static method using the :: method will cause an E_STRICT level error.
Now let’s focus on Article 4.
Operating environment: (Win32) PHP/5.3.3

The code is as follows Copy code
 代码如下 复制代码

class Foo{ 
    
    public static $my_static = 'foo';//声明一个静态成员 
    
    
    public function staticValue() {//静态方法 
        return self::$my_static;// 
    } 
    public function run(){//非静态方法 
      return "abc
"; 
    } 
    public  function callrun() { 
        return self::run();//用self::方式调用一个非静态方法 
            
    } 
     

    
echo Foo::$my_static . "
"; 
    
echo Foo::run();//用className::方法名调用非静态方法 
echo Foo::callrun();

class Foo{

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

     

Public function staticValue() {//Static method
          return self::$my_static;// 

}  
 代码如下 复制代码

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;
 }

Public function run(){//Non-static method

Return "abc
";

}  

Public function callrun() {            return self::run();//Use self:: to call a non-static method                                                                           }  

       

}

     
 代码如下 复制代码

01 

02 function test()

03 {

04 static $var1 =1;

05 $var1 +=2;

06 echo $var1 . ' ' ;

07 }

08

09 test();

10 test();

11 test();

12 ?> 
运行结果如下:

3 5 7

echo Foo::$my_static . "
";       echo Foo::run();//Use className::method name to call non-static method echo Foo::callrun();
static keyword for interview questions Question code: Write the operation result of the following code ( )
The code is as follows Copy code
<🎜>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, I thought it was a simple self-added calculation: I thought the result was 400, but the answer turned out to be wrong. Finally, I took a closer look, typed the code, and found out it was 500 after running it. Printed $i+=$n twice; calculated the previous $i, once was 100, and once was 200; I know that the problem is static. Then Baidu looked at the static keyword and suddenly realized it. <🎜> <🎜><🎜>static keyword function: <🎜><🎜> <🎜>The scope of use of static variables in PHP is wider. Not only can we add the static modifier in front of a class, method or variable, we can even add the static keyword to the internal variables of the function. The value of a variable with the static modifier added will not be lost even after the function is executed. That is to say, the variable will still remember the original value the next time the function is called. Such as: <🎜>
The code is as follows Copy code
<🎜>01 <🎜>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 results are as follows: 3 5 7

In summary:

What is the difference between static global variables and ordinary global variables:
Static global variables are only initialized once to prevent them from being referenced in other file units;
What is the difference between static local variables and ordinary local variables:
Static local variables are only initialized once, and the next time is based on the previous result value;
What is the difference between static functions and ordinary functions:
Static functions have only one copy in memory, while ordinary functions maintain a copy for each call

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632692.htmlTechArticlestatic is a static variable in php, it can define functions, and the variable is a global static variable, then we are What will happen if static is added in front of a function or variable...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template