declares class members or methods as static, so you can access them directly without instantiating the class. Static members (except static methods) cannot be accessed through an object.
In order to be compatible with PHP4, if "visibility" is not specified, properties and methods default to public.
Since static methods do not require an object to be called, the pseudo variable $this is not available in static methods.
Static properties cannot be accessed by objects through the -> operator.
Calling a non-static method using the :: method will result in an E_STRICT level error.
Like all other PHP static variables, static properties can only be initialized to a character value or a constant, and expressions cannot be used. So you can initialize a static property to an integer or array, but it cannot point to another variable or function return value, nor to an object.
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
<?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() . " "; ?>
Example #2 Static method code example
<?php class Foo { public static function aStaticMethod() { // ... } } Foo::aStaticMethod(); $classname = 'Foo'; $classname::aStaticMethod(); // As of PHP 5.3.0 ?>
A summary of static variables and static methods in static
Static variables
Static variables are variables that only exist in the scope of a function. However, the value of such a variable will not be lost after the function is executed. That is to say, the variable will still remember the original value the next time the function is called. . To define a variable as static, just add the static keyword before the variable.
In a class, the static keyword has two main uses, one is to define static members, and the other is to define static methods. Within a class, you can use the scope qualifier (::) to access variables at different levels of scope.
Static method
There is an important difference between static and non-static methods: when calling a static method, you no longer need to own an instance of the class.
Principles for using static methods and non-static methods: First, if a method does not contain the $this variable, it should be a static method; if you do not need an instance of the class, you may also use a static class, which can eliminate the need for an instance. Chemical work. In addition, the $this variable cannot be used in a static method, because the static method does not belong to a specific instance.