/* +-------------------------------------------------- --------------------------------+ | = This article is read by Haohappy> | = Classes and Objects 1 Chapter's notes | = Translation + personal experience | = To avoid unnecessary trouble, please do not reprint, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +------- -------------------------------------------------- -----------------------+ */ Section 7 - Static members of classes Static members of classes are different from ordinary class members: Static members are different from objects Instances have nothing to do, only related to the class itself. They are used to implement the functions and data to be encapsulated by the class, but do not include the functions and data of specific objects. Static members include static methods and static properties. Static properties contain the data to be encapsulated in the class , can be shared by all instances of a class. In fact, in addition to belonging to a fixed class and restricting access methods, the static properties of a class are very similar to the global variables of a function. We use a static property Counter::$count in the following example. It belongs to the Counter class, not to any Counter instance. You cannot use this to refer to it, but you can use self or other valid naming expressions. In the example, the getCount method returns self::$count, not Counter:: $count. Static methods implement functions that need to be encapsulated by the class and have nothing to do with specific objects. Static methods are very similar to global functions. Static methods can fully access the properties of the class or be accessed by instances of the object, regardless of the access qualifiers Whether it is something. In the 6.3 example, getCount is an ordinary method, called with ->. PHP creates a this variable, although the method is not used. However, getCount does not belong to any object. In some cases, we even want If you call it when there is no valid object, then you should use static methods. PHP will not create this variable inside static methods, even if you call them from an object. Example 6.7 comes from 6.3 changing getCount to a static method. Static key Words cannot prevent an instance from using the -> operator to call getCount, but PHP will not create this variable inside the method. If you use this-> to call, an error will occur. //6.3 Example refers to Section 4-Constructor and The example in the destructor (see the previous article), by comparing the two examples, you can have a good grasp of the difference between //static methods and ordinary methods. You can write a method to show whether it is created by judging whether this is established. Called statically or non-statically. Of course, if you use the static keyword, this method will always be static no matter how it is called. Your class can also define constant properties, without using public static, just use const keyword. Constant properties are always static. They are properties of the class, not of the object that instantiates the class. Listing 6.7 Static members