The static keyword declares that an attribute or method is related to the class, rather than to a specific instance of the class. Therefore, this type of attribute or method is also called "class attribute" or "class method".
If access control permissions allow, you can call it directly using the class name plus two colons "::" without creating an object of this class.
The static keyword can be used to modify variables and methods.
You can directly access the static properties and static methods in the class without instantiation.
Static properties and methods can only access static properties and methods, and non-static properties and methods cannot be accessed by class. Because when static properties and methods are created, there may not yet be any instances of this class that can be called.
Static attributes have only one copy in memory and are shared by all instances.
Use the self:: keyword to access static members of the current class.
Public properties of static properties
All instances of a class share static properties in the class.
In other words, even if there are multiple instances in the memory, there is only one copy of the static attributes.
In the following example, a counter $count attribute is set, with private and static modifications. In this way, the outside world cannot directly access the $count property. As a result of the program running, we also see that multiple instances are using the same static $count attribute.
Static attribute calls directly
Static properties can be used directly without instantiation, and can be used directly before the class is created.
The method used is class name::static property name.
Copy code
The code is as follows:
The area with a radius of 3 is
28.26
The area with a radius of 3 is
28.2743338823
The class is not created, and the static attributes can be used directly. When are static properties created in memory? I haven't seen any relevant information in PHP. Citing concepts in Java to explain should also be universal.
Static properties and methods, created when the class is called. When a class is called, it means that the class is created or any static member in the class is called.
Static method
The method used is class name:: static method name.
Let’s continue writing this Math class to perform mathematical calculations. We design a method to calculate the maximum value. Since it is a mathematical operation, we do not need to instantiate this class. It would be much more convenient if this method can be taken and used.
We designed this class just to demonstrate the static method. PHP provides the max() function to compare numerical values.
Copy code The code is as follows:Program running results:
Displays that the maximum value in $a and $b is
99
Displays that the maximum value in $a and $b is
100
Static How to call static method
The first example, when a static method calls other static methods, use the class name directly.
Program running result:
1
2
Displays that the maximum value among 99 77 88 is
99
You can also use self:: to call other static methods in the current class. (Suggestion)
Program running result:
1
2
Displays that the maximum value among 99 77 88 is
99
Static method calls static property
Use class name::static property name to call the static properties in this class.
Program execution result:
1
The area of a circle with radius 3 is 28.26
Use self:: to call the static properties of this class. (Suggestion)
Program running results:
1
The area of a circle with radius 3 is 28.26
Static methods cannot call non-static properties
Static methods cannot call non-static properties. Non-static properties cannot be called using self::.
Program running result:
1
Fatal error: Undefined class constant 'pi' in E:PHPProjectstest.php on line 7
You also cannot use $this to get the value of a non-static property.
Program running result:
1
Fatal error: Using $this when not in object context in E:PHPProjectstest.php on line 7
Static method calls non-static method
In PHP5, the $this identifier cannot be used in static methods to call non-static methods.
Program running result:
Displays that the maximum value among 99 77 188 is
Fatal error: Using $this when not in object context in E:test.php on line 10
When a non-static method in a class is called by self::, the system will automatically convert this method into a static method.
The following code was executed and produced results. Because the Max method is converted into a static method by the system.
In the following example, we let the static method Max3 use self:: to call the non-static method Max, and let the non-static method Max call the non-static property $pi through $this.
An error was reported when running. This error is the same as the previous example 3-1-9.php. This time, the non-static method Max reported an error of calling non-static properties by a static method.
This proves something. The non-static method Max we defined here is automatically converted into a static method by the system.
Copy code
The code is as follows:
The program running result:
1
2
The maximum value shown in 99 77 188 is
Fatal error: Access to undeclared static property: Math::$pi in E: PHPProjectstest.php on line 7