Home >Backend Development >PHP Tutorial >static_PHP tutorial in php
Static members are a type of class variable, which can be thought of as belonging to the entire class rather than to an instance of the class. Different from general instance variables, static members only retain one variable value, and this variable value is valid for all instances, that is, all instances share this member.
$this only represents the current instance of the class, while self:: represents the class itself. This operator cannot be used in code outside the class, and it cannot identify its position in the inheritance tree hierarchy. That is to say, when using the self scope in an extended class, self can call methods declared in the base class, but it always calls methods that have been overridden in the extended class. Unlike $this, when using static variables, you must add the $ symbol after the scope qualifier.
In the extended class, when the base class method is overridden, use the parent scope to call the method defined in the base class. Static members can also only belong to the parent class. If a member is declared in both the subclass and the parent class, you can also use parant:: to access the variables in the parent class in the subclass. In this case, the static members of the parent class and the static members of the subclass hold different values.
You can write the name of the class on the left side of the :: operator to statically access a member to avoid creating an instance of the class. Not only does it eliminate the need to instantiate a class, it is also more efficient because each instance of the class takes up a small portion of system resources.
When using the :: operator to access member variables, you need to pay attention to the use of the $ symbol again. Because PHP currently does not support the use of dynamic static variables, that is to say, it does not support mutable static variables. When using $this->$var, the member being accessed is the value of the variable contained in $var. Instead of using the $ symbol to access a variable, you are actually looking for a constant of the class, and constants cannot be accessed through $this.
The static::scope proposed in PHP6 eliminates the need for us to use self:: and parent::. When you want to point to the final class that implements the function, you can use static::. This qualifier will calculate the members of the last class in the inheritance hierarchy immediately before the code is executed. One process is called lazy binding, which allows us to override a static variable in a child class and also access the final member from a function declared in the parent class.
Sometimes, it may be necessary to create fields and methods that are shared by all class instances, that are relevant to all class instances, but cannot be called by any specific object. For example, suppose you want to write a class that tracks the number of visitors to a web page. You definitely don’t want to reset the number of visitors to 0 every time you instantiate the class. At this time, you can set the field to static scope:
<?php class visitors { private static $visitors = 0; function __construct() { self::$visitors++; } static function getVisitors() { return self::$visitors; } } /* Instantiate the visitors class. */ $visits = new visitors(); echo visitors::getVisitors()."<br />"; /* Instantiate another visitors class. */ $visits2 = new visitors(); echo visitors::getVisitors()."<br />"; ?>
Program execution result:
1
2
Because the $visitors field is declared static, any changes to its value will be reflected in all instantiated objects. Also note that static fields and methods should be referenced using the self keyword and class name, not via this and the arrow operator. This is because referencing static fields using "normal" methods is not possible and will result in syntax errors.
You cannot use $this in a class to refer to a static field.
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.
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.