PHP5 Object Model [7] - Static member of the class

zhaozj2021-02-16  126

Section 7 - Static members of the class of static members and the general class members are different: static members have nothing to do with the instance of the object, only related to the class itself. They are used to implement the functions and data to be packaged, but do not include specific Objects and data. Static members include static methods and static properties. Static properties include data to be encapsulated in the class, can be shared by all classes. In fact, in addition to a fixed class and restrict access, classes The static property is very similar to the global variable of the function. We use a static property counter: $ count in the following example. It belongs to the Counter class, instead of any instance of any Counter. You can't use this to reference it, but you can use Self Or other effective naming expressions. In the example, getCount method returns Self :: $ count, not counter: $ count. Static method implements the functionality of the package, unrelated to a specific object. Static method is very similar to the global Function. Static method can fully access the properties of the class, or can be accessed by an instance of an object, regardless of whether or not the regions accessed. In 6.3 cases, getCount is an ordinary method, with-> to call. PHP create one The THIS variable, although the method is not used. However, getCount does not belong to any object. In some cases, we even want to call it without a valid object, then you should use a static method. PHP will not build this inside the static method Variables, even if you call them from an object. Example 6.7 Change getCount is a static method from 6.3. Static keyword cannot block a instance -> operator from calling getCount, but PHP will not establish a THIS variable inside the method. If You use this-> to call, it will be wrong .//6.3 refers to the example of the fourth-section-constructor and the description function (see forebase), you can master // The difference between the Static method and the common method. You can write a method to display whether it is set to be static or non-static by judging whether it is established. Of course, if you use the static keyword, no matter how it is called, this The method is always static. Your class can also define constant attributes, do not need to use public static, just use const keywords. Constant properties are always static. They are attributes of classes, rather than instantification Objects' properties. Lloyd 6.7 Static Members

Class counter = 0; const line = 2.0; function __construct () {self: $ count ;} function __destruct () {Self: $ count--;} static function getCount () {Return Self: : $ count;}}; // Create an instance, __construct () will execute $ c = new counter (); // Output 1 print (); "N"); // Output class Version Properties Print ("Version Used:". Counter :: Version. "N");?>

转载请注明原文地址:https://www.9cbs.com/read-8139.html

New Post(0)