The static keyword is used to modify properties, methods, which are called properties, methods as static properties, and static methods.
The static keyword declares that a property or method is related to a class and not to a particular instance of a class, so that such a property or method is also known as a class property or class method
If the access Control permission allows, you do not have to create the class object and directly use the class name plus two colons "::" Call.
The static keyword can be used to modify variables, methods.
without instantiation, you can directly access static properties and static methods in the class.
Static properties and methods can only access static properties and methods, and non-static properties and methods cannot be accessed by class. Because static properties and methods are created, there may not be any instances of this class being tuned
use.
static property, only one copy in memory, shared for all instances.
Use the self:: keyword to access the static members of the current class.
all instances of a class, a static property in a common class.
that is, even if there are multiple instances in memory, there is only one copy of the static property.
The following example sets a counter $count property, setting the private and static adornments.
this way, the $count property is not directly accessible to the outside world. And the results of the program run we also see multiple instances using the same static $count property.
<?php class User {private static $count = 0;//record the logon status of all users. Public Function construct () {self:: $count = self:: $count + 1; } public Function GetCount () {return self:: $count; } public Function destruct () {self:: $count = self:: $count-1; }} $user 1 = new user (); $user 2 = new User (); $user 3 = new User (); echo "Now here has". $user 1->getcount (). "User"; echo "<br/>"; Unset ($user 3); echo "Now here has". $user 1->getcount (). "User";
Static properties are called directly
Static properties can be used directly without instantiation and can be used directly when the class is not created.
Used in the following way: Class Name:: Static property name
<?php class Math {public static $pi = 3.14; } Find the area of a garden with a radius of 3. $r = 3; echo "radius is the area of the $r is <br/>"; echo Math:: $pi * $r * $R; echo "<br/><br/>"; Here I think 3.14 is not accurate enough, I set it more precisely. Math:: $pi = 3.141592653589793; echo "radius is the area of the $r is <br/>"; echo Math:: $pi * $r * $R; ? >
class is not created, static properties can be used directly. When was the static property created in memory? No data was found in PHP. Refer to the concepts in Java to explain that it should also be universal
. Static properties and methods that are created when the class is invoked.
Static Methods
static methods do not require that the class be instantiated to be used directly.
the way to use the class name:: Static method Name
Let's continue writing this math class for mathematical calculations. We design a method to figure out the maximum value. Since it is a mathematical operation, we do not need to instantiate this class, if this method
It's much more convenient to take it over. We are just this class designed to demonstrate the static method. The max () function comparison value is provided in PHP.
view plaincopy to clipboardprint?<?php class Math {public static Fu Nction Max ($num 1, $num 2) {return $num 1 > $num 2? $num 1: $num 2; }} $a = 99; $b = 88; echo "Displays the maximum value in $a and $b is"; echo "<br/>"; Echo Math::max ($a, $b); echo "<br/>"; echo "<br/>"; echo "<br/>"; $a = 99; $b = 100; echo "Displays the maximum value in $a and $b is"; echo "<br/>"; Echo Math::max ($a, $b);
< /span>
First example, when a static method calls another static method, use Self::
<?php//The math class that implements the maximum value comparison. Class Math {public static function Max ($num 1, $num 2) {return $num 1 > $num 2? $num 1: $num 2; public static function Max3 ($num 1, $num 2, $num 3) {$num 1 = Self::max ($num 1, $num 2); $num 2 = Self::max ($num 2, $num 3); $num 1 = Self::max ($num 1, $num 2); return $num 1; }} $a = 99; $b = 77; $c = 88; echo "Displays the maximum value in the $a $b $c is"; echo "<br/>"; Echo math::max3 ($a, $b, $c);
static method call static property
Use self:: Call the static property of this class.
<?php // class Circle {public static $pi = 3.14; public static function Circleacreage ($r) { return $r * $r * self:: $pi; } } $r = 3; echo "Radius $r the area of the circle is". Circle::circleacreage ($r); ? >
A static method cannot invoke a non-static property. You cannot use Self:: Call a non-static property.
<?php //This method is the wrong class Circle {public $pi = 3.14; public static function Circleacreage ($r) { return $r * $r * self::p i; } } $r = 3; echo "Radius $r the area of the circle is". Circle::circleacreage ($r); ? >
< /span>
static method call non-static method
PHP5, a non-static method cannot be invoked in a static method using the $this identity.
<?php//The math class that implements the maximum value comparison. Class Math {public Function Max ($num 1, $num 2) {echo "bad<br/>"; Return $num 1 > $num 2? $num 1: $num 2; public static function Max3 ($num 1, $num 2, $num 3) {$num 1 = $this->max ($num 1, $num 2); $num 2 = $this->max ($num 2, $num 3); $num 1 = $this->max ($num 1, $num 2); return $num 1; }} $a = 99; $b = 77; $c = 188; echo "Displays the maximum value in the $a $b $c is"; echo "<br/>"; Echo math::max3 ($a, $b, $c); The same one will make an error.
< /span>
When a non-static method in a class is self:: When called, the system automatically converts this method to a static method.
<?php //The math class that implements the maximum value comparison. class Math {Public function Max ($num 1, $num 2) { return $num 1 > $num 2? $num 1: $num 2; } public static function Max3 ($num 1, $num 2, $num 3) { $num 1 = Self::max ($num 1, $num 2); $num 2 = Self::max ($num 2, $num 3); $num 1 = Self::max ($num 1, $num 2); return $num 1; } } $a =; $b =; $c = 188; echo "Displays the maximum value in the $a $b $c is"; echo "<br/>"; Echo math::max3 ($a, $b, $c); ? >