The static keyword can be used to identify a member in a class as static. It can be used to identify Member attributes or member methods. Ordinary Members exist as object attributes. Take the person class as an example. If there is a member attribute "$ conuntry = 'China'" in person, any object in the person class has its own $ country attribute, so there is no interference between objects. Static members exist as attributes of the entire class. If the $ country attribute is identified by the static keyword, no matter how many objects (or even no objects) are created through the person class ), this static member is always unique and shared among multiple objects. Because the static ID is used
The member attributes belong to the class, so they are irrelevant to the object instance and other classes. The static attributes of the class are very similar to the global variables of the function. The static members in the class directly access the object without using the class name. The format is as follows:
Class Name: static member attribute name;
Class Name: static member method name ()
You can also use the keyword "self" in the member Methods declared in the class to access other static members. Because static members belong to the class and do not belong to any object, you cannot use $ this to reference it, but the self keyword provided to us in PHP, is used by the class members to represent the keywords of this class. Format;
SELF: static member attribute name; // use this method in the class member method to access static member attributes in this class
SELF: static member method name (); // use this method to access the static member method in the class member Method
If you are a static member in the external category class of the class, you can use the object to reference and use the class name for access, but generally choose to use the class name for access. If you access other static members in the member methods of the class, the class name is usually accessed in the form of self. It is recommended that you do not directly use the class name. In the following example, A myclass class is declared as a static member to allow the Count attribute in the class to be shared in each object, used to count the total number of objects created through the myclass class. The Code is as follows:
<? PHP
Class myclass {
Static $ count;
Function _ Contruct (){
SELF: $ count ++;
}
Static function getcount (){
}
Static function gecount ()
Return self: $ count;
}
}
Myclass: $ COUNT = 0;
$ Myc1 = new myclass ();
$ Myc2 = new myclass ();
$ Myc3 = new myclass ();
Echo myclass: getcount ();
Echo $ myc3-> getcount ();
In the myclass class in the above example, both the constructor and the member method getcount () Use self to access the static attribute count marked as static in this class, and use the static attribute in the class name category class outside the class. You can see that static members in the same class are shared in each object. Each time you create an object, the static attribute count increases by 1. This is used to count the number of instantiated objects.
In addition, when using static methods, you must note that only static members can be accessed in static methods. Because non-static members can be accessed only through object reference, it is usually completed using $ this. Static methods can also be accessed directly using class names if the object does not exist. Without an object, no $ this reference is available. Without $ this reference, non-static members in the category cannot be accessed, however, you can use the class name or self to access static members in non-static methods.