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 ordinaryfunction 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 = '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() . " "; ?>
<?php class Foo { public static function aStaticMethod() { // ... } } Foo::aStaticMethod(); $classname = 'Foo'; $classname::aStaticMethod(); // As of PHP 5.3.0 ?>
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 = 'foo';//声明一个静态成员 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();
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!