The following small series will give you an analysis of the usage differences between php static methods and non-static methods. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the small Editor. in php programming, the static keyword declares that an attribute or method is related to a class, rather than a specific instance of the class. therefore, such attributes or methods are also called "class attributes" or "class methods"
If the access control permission permits, you can directly use the class name with two colons ":" Instead of creating the class object.
Static keywords can be used to modify variables and methods.
Without instantiation, you can directly invoke the static attributes and static methods in the class.
Static attributes and methods can only access static attributes and methods, but cannot access non-static attributes and methods. Because no instance of this class can be called when static attributes and methods are created.
Static attributes, which are shared by all instances in the memory.
Use the self: keyword to access the static members of the current class.
All instances of a class share static attributes of the class.
That is, even if there are multiple instances in the memory, there is only one static attribute.
In this example, a counter $ count attribute is set, and private and static modifiers are set.
In this way, you cannot directly access the $ count attribute. The running result also shows that multiple instances use the same static $ count attribute.
<? Php class user {private static $ count = 0; // records 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 ;}$ user1 = new user (); $ user2 = new user (); $ user3 = new user (); echo "now here have ". $ user1-> getcount (). "user"; echo"
"; Unset ($ user3); echo" now here have ". $ user1-> getcount ()." user ";?>
2. direct call of static attributes
Static attributes can be directly used without instantiation. they can be used directly before a class is created.
Usage:
Class name: static property name
<? Php class math {public static $ pi = 3.14;} // calculate the area of the garden with a radius of 3. $ R = 3; echo "the radius is $ r and the area is
"; Echo math: $ pi * $ r; echo"
"; // Here I think 3.14 is not accurate enough. I set it more accurately. Math: $ pi = 3.141592653589793; echo "the radius is $ r and the area is
"; Echo math: $ pi * $ r;?>
Class is not created, and static attributes can be directly used. When will the static property be created in the memory?
No relevant information is found in php.
By referencing the concepts in java, it should also be universal. Static attributes and methods, which are created when a class is called.
3. static method
Static methods can be directly used without the class being instantiated.
The method is class name: static method name
Continue to write this math class for mathematical computation.
Design a method to calculate the maximum value. Since it is a mathematical operation, there is no need to instantiate this class. if this method can be used, it will be much more convenient.
This is just the class designed to demonstrate the static method. The max () function is provided in php to compare values.
<? Php class math {public static function max ($ num1, $ num2) {return $ num1> $ num2? $ Num1: $ num2 ;}}$ a = 99; $ B = 88; echo "shows that the maximum values in $ a and $ B are"; echo"
"; Echo math: max ($ a, $ B); echo"
"; Echo"
"; Echo"
"; $ A = 99; $ B = 100; echo" shows that the maximum value of $ a and $ B is "; echo"
"; Echo math: max ($ a, $ B);?>
How to call static methods
In the first example, when a static method calls other static methods, use self ::
<? Php // math class for maximum value comparison. Class math {public static function max ($ num1, $ num2) {return $ num1> $ num2? $ Num1: $ num2;} public static function max3 ($ num1, $ num2, $ num3) {$ num1 = self: max ($ num1, $ num2 ); $ num2 = self: max ($ num2, $ num3); $ num1 = self: max ($ num1, $ num2); return $ num1 ;}} $ a = 99; $ B = 77; $ c = 88; echo "shows that the maximum value in $ a $ B $ c is"; echo"
"; Echo math: max3 ($ a, $ B, $ c);?>
Static method call static attributes
Use self: To call the static attributes of this class.
<? Php // class circle {public static $ pi = 3.14; public static function circleacreage ($ r) {return $ r * self: $ pi ;}} $ r = 3; echo "the area of the circle with a radius of $ r is ". circle: circleacreage ($ r);?>
Static methods cannot call non-static attributes. You cannot use self: to call non-static attributes.
<? Php // This method is incorrect class circle {public $ pi = 3.14; public static function circleacreage ($ r) {return $ r * self: pi ;}} $ r = 3; echo "the area of the circle with a radius of $ r is ". circle: circleacreage ($ r);?>
You cannot use $ this to obtain non-static attribute values.
Static method call non-static method
In php5, you cannot use $ this to call non-static methods in static methods.
<? Php // math class for maximum value comparison. Class math {public function max ($ num1, $ num2) {echo "bad
"; Return $ num1> $ num2? $ Num1: $ num2;} public static function max3 ($ num1, $ num2, $ num3) {$ num1 = $ this-> max ($ num1, $ num2 ); $ num2 = $ this-> max ($ num2, $ num3); $ num1 = $ this-> max ($ num1, $ num2); return $ num1 ;}} $ a = 99; $ B = 77; $ c = 188; echo "shows that the maximum value in $ a $ B $ c is"; echo"
"; Echo math: max3 ($ a, $ B, $ c); // The same error will be reported?>
When a class has a non-static method called by self:, the system automatically converts this method to a static method.
<? Php // math class for maximum value comparison. Class math {public function max ($ num1, $ num2) {return $ num1> $ num2? $ Num1: $ num2;} public static function max3 ($ num1, $ num2, $ num3) {$ num1 = self: max ($ num1, $ num2 ); $ num2 = self: max ($ num2, $ num3); $ num1 = self: max ($ num1, $ num2); return $ num1 ;}} $ a = 99; $ B = 77; $ c = 188; echo "shows that the maximum value in $ a $ B $ c is"; echo"
"; Echo math: max3 ($ a, $ B, $ c);?>
The usage difference between php static and non-static methods in this article is the full content shared by the editor. I hope to give you a reference and support.