Home >Backend Development >PHP Problem >What is the function of static keyword in php

What is the function of static keyword in php

王林
王林Original
2020-06-29 16:37:006487browse

The functions of the static keyword in php are: 1. Put it inside the function to modify the variable, and the variable value will still be saved after the function is executed; 2. Put it in the class to modify the attributes or methods. If the modification is of a class attribute, the value is retained; 3. Modify the variable in the method of the class; 4. Modify the variable in the global scope.

What is the function of static keyword in php

The functions of the static keyword are as follows:

1. Put it inside the function to modify the variable;

2. Put it in the class to modify the attributes or methods;

3. Put it in the class method to modify the variables;

4. Modify variables in the global scope; the different meanings represented by the

keywords are as follows:

1. After the function is executed, the variable value is still saved

As shown below:

<?php
function testStatic() {
    static $val = 1;
    echo $val;
    $val++;
}
testStatic();   //output 1
testStatic();   //output 2
testStatic();   //output 3
?>

2. Modified attributes or methods can be accessed through the class name. If the modified attribute is a class attribute, the reserved value

is as follows:

<?php
class Person {
    static $id = 0;
 
    function __construct() {
        self::$id++;
    }
 
    static function getId() {
        return self::$id;
    }
}
echo Person::$id;   //output 0
echo "<br/>";
 
$p1=new Person();
$p2=new Person();
$p3=new Person();
 
echo Person::$id;   //output 3
?>

3. The variables in the method of modifying the class

are as follows:

<?php
class Person {
    static function tellAge() {
        static $age = 0;
        $age++;
        echo "The age is: $age
";
    }
}
echo Person::tellAge(); //output &#39;The age is: 1&#39;
echo Person::tellAge(); //output &#39;The age is: 2&#39;
echo Person::tellAge(); //output &#39;The age is: 3&#39;
echo Person::tellAge(); //output &#39;The age is: 4&#39;
?>

4. The variables that modify the global scope have no practical meaning

are as follows: Display:

<?php
static $name = 1;
$name++;
echo $name;
?>
另外:考虑到PHP变量作用域

<?php
include &#39;ChromePhp.php&#39;;
 
$age=0;
$age++;
 
function test1() {
    static $age = 100;
    $age++;
    ChromePhp::log($age);  //output 101
}
 
function test2() {
    static $age = 1000;
    $age++;
    ChromePhp::log($age); //output 1001
}
 
test1();
test2();
ChromePhp::log($age); //outpuut 1
?>

It can be seen that these three variables do not affect each other. In addition, there are only global scope and function scope in PHP, not block scope.

If you want to learn more related knowledge, welcome to visit php Chinese website.

The above is the detailed content of What is the function of static keyword in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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