The rules for static methods are the same as static variables. You can use the Ststic keyword to identify a method as a static method, using the name and scope of the class to qualify the operator:: You can access a static method.
One important difference between static and non-static methods is that when you call a static method, we don't need to create an instance of the class.
Program List: Using class-Named parameters
The non-inherited static problem can be solved by using the class famous as parameter.
Program Run Result:
I ' m Apple
Program List: Overriding base class methods
Overrides a method of a base class in a derived class.
Program Run Result:
Apple ' s color is red
Program List: Use of static arrays
Both static and const scopes can be accessed using:: Operators, and if you want to access an array using:: operator, you need to declare the array as static in advance.
' Red ', ' color2 ' = ' yellow ');} Class apple{public function __construct () { var_dump (Fruit:: $color);} } Class banana{public function __construct () { Fruit:: $color = FALSE; }} New Apple (); Prints Array (2) {["Color1"]=> string (3) "Red" ["Color2"]=> string (6) "Yellow"} echo '
'; new Banana (); new Apple (); prints bool (false)?>
Program Run Result:
Array (2) {["Color1"]=> string (3) "Red" ["Color2"]=> string (6) "Yellow"} bool (FALSE)
Program list: One more singleton mode
Static is really cool, the following program shows how to get an already existing instance.
Value = $value; } public static function getinstance () { if (self:: $instance = = null) { echo]
New
"; Self:: $instance = new Singleton ("values"); } else { echo '
Old
"; } Return self:: $instance; }} $x = Singleton::getinstance (); Var_dump ($x); Returns the new object$y = Singleton::getinstance (); Var_dump ($y); Returns the existing object?>
Program Run Result:
NewObject (Singleton) #1 (1) {["Value:private"]=> string (6) "Values"} oldobject (Singleton) #1 (1) {["Value:private"]= > string (6) "Values"}
http://www.bkjia.com/PHPjc/752389.html www.bkjia.com true http://www.bkjia.com/PHPjc/752389.html techarticle the rules for static methods are the same as static variables. You can use the Ststic keyword to identify a method as a static method, using the name and scope of the class to qualify the operator:: You can access static methods ...