On the static member depth analysis of PHP class

Source: Internet
Author: User
A static member of a class is different from a normal class member: A static member is not related to an instance of an object, but only to the class itself. They are used to implement the functionality and data that the class wants to encapsulate, but do not include features and data for a particular object, static members include static methods and static properties.

A static property contains the data to encapsulate in the class, which can be shared by instances of all classes. In fact, in addition to belonging to a fixed class and restricting access, the static properties of the class are very similar to the global variables of the function.

In the following example, we used a static property counter:: $count. It belongs to the counter class and does not belong to any counter instance. You cannot use this to refer to it, but you can use self or another valid named expression. In the example, the GetCount method returns self: $count instead of counter:: $count.

The static party law implements the functionality that the class needs to encapsulate, regardless of the specific object. A static method is very similar to a global function. A static method can have full access to the properties of a class, or it can be accessed by an instance of an object, regardless of whether the qualifier is accessed.

In the previous example, GetCount is a common method, called with. PHP builds a 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 when there are no valid objects, so we should use a static method. PHP will not build this variable inside a static method, even if you call them from an object.

Example 6.7 changes the GetCount from 6.3 to a static method. The static keyword does not prevent an instance from invoking GetCount with the operator, but PHP does not establish the this variable inside the method. If you use This-> to invoke, an error will occur.

6.3 Cases refer to section fourth-examples of constructors and destructors (see previous article), and by comparison of two examples, you can get a good grasp of
The difference between a static method and a normal method.

You can write a method by judging whether this is established to show whether it is called statically or statically. Of course, if you use the static keyword, no matter how it is called, this method is always static.

Your class can also define a constant property, and you do not need to use public static, just use the const keyword. Constant properties are always static. They are properties of the class, not the properties of the object that instantiates the class.

Listing 6.7 Static Members


<?phpclass Counter {private static $count = 0;   Const VERSION = 2.0;   function construct () {self:: $count + +;   } function destruct () {self:: $count-;   } static function GetCount () {return self:: $count;  }  };  When an instance is created, construct () executes $c = new Counter (); Output 1 print (Counter::getcount ().  "N"); The version property of the output class print ("version used:".) Counter::version.  "N"); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.