Home > Backend Development > PHP Tutorial > Detailed explanation of usage examples of static keyword in functions in PHP

Detailed explanation of usage examples of static keyword in functions in PHP

伊谢尔伦
Release: 2023-03-11 13:46:02
Original
1270 people have browsed it

static is a static variable in php. He can define functions. The variable is a global static variable. So what impact will we add static in front of the function or variable to the function and variable? Well, let’s take a look together.  

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 static function is different from the ordinary

function scope , only in this file. Functions used only in the current source file should be described 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 want to use these functions must include this header file.

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

Example #1 Static member code example

<?php
class Foo
{
    public static $my_static = &#39;foo&#39;;
    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 = &#39;Foo&#39;;
print $classname::$my_static . " "; // PHP 5.3.0之后可以动态调用
print Bar::$my_static . " ";
$bar = new Bar();
print $bar->fooStatic() . " ";
?>
Copy after login

Example #2 Static method code example

<?php
class Foo {
    public static function aStaticMethod() {
        // ...
    }
}
Foo::aStaticMethod();
$classname = &#39;Foo&#39;;
$classname::aStaticMethod(); // As of PHP 5.3.0
?>
Copy after login

About the use of the

Static keyword in the class, The PHP manual gives the following conventions:

1. Declare class members or methods as static, and you can access them 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.
Running environment: (Win32) PHP/5.3.3

class Foo{       
    public static $my_static = &#39;foo&#39;;//声明一个静态成员  
    public function staticValue() {//静态方法  
       return self::$my_static;//  
    }  
    public function run(){//非静态方法  
      return "abc <br>";  
    }  
    public  function callrun() {  
        return self::run();//用self::方式调用一个非静态方法  
             
    }  
      
}  
     
echo Foo::$my_static . "<br >";  
     
echo Foo::run();//用className::方法名调用非静态方法  
echo Foo::callrun();
Copy after login

static keyword function:

The scope of use of static variables in PHP is wider. We can not only use it in classes, Add the static modifier in front of the method or variable, and we can even add the static keyword to the variables inside 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.




The above is the detailed content of Detailed explanation of usage examples of static keyword in functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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