Section 7 static members of a class -- ClassesandObjectsinPHP5 [7]

Source: Internet
Author: User
Section 7: static members of a class are different from those of a general class. static members are irrelevant to the instance of the object and only related to the class. they are used to implement the functions and data to be encapsulated by classes, but do not include the functions and data of specific objects. static members include static methods and static attributes. static attributes include the data to be encapsulated in the class, which can be SyntaxHighlighter. all (); Section 7: static members of a class

Static members of a class are different from general class members: static members are irrelevant to the instance of the object and only related to the class itself. they are used to implement the functions and data to be encapsulated by classes, but do not include the functions and data of specific objects. static members include static methods and static attributes.

Static attributes include the data to be encapsulated in the class, which can be shared by all instances of the class. in fact, in addition to a fixed class and restricted access methods, the static attributes of the class are very similar to the global variables of the function.

In the following example, we use a static attribute Counter: $ count. it belongs to the Counter class and does not belong to any Counter instance. you cannot use this to reference it, but you can use self or other valid naming expressions. in the example, the getCount method returns self: $ count instead of Counter: $ count.

Static methods implement the functions that the class needs to encapsulate and have nothing to do with specific objects. static methods are very similar to global functions. static methods can be completely accessed by the Category attributes or by the instance of the object, regardless of whether the access qualifier is.

In the first example, getCount is a common method and is called with->. PHP creates a this variable, although the method is not used. however, getCount does not belong to any object. in some cases, if we want to call a valid object even if it does not exist, we should use static methods. PHP will not create this variable within the static method, even if you call them from an object.

In example 6.7, the getCount method is changed from 6.3 to static. the Static keyword cannot prevent an instance from using the-> operator to call getCount, but PHP will not create this variable within the method. if you call this->, an error occurs.

// 6.3 Examples refer to section 4-examples in constructor and Destructor (see the previous article). by comparing the two examples, you can have a good grasp of them.
// Differences between static and common methods.

You can write a method to determine whether this is created to show whether it is called statically or not. of course, if you use the static keyword, no matter how it is called, this method is always static.

Your class can also define constant attributes, without using public static, you only need to use the const keyword. constant attributes are always static. they are the attributes of the class, rather than the attributes of the objects of the class.

Listing 6.7 Static members

 "); // Print (" Version used: ". Counter: VERSION ."
");?>

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.