Section 7 static class member ClassesandObjectsinPHP57

Source: Internet
Author: User
/* + Tips + | this document is Haohappy-read | notes in the ClassesandObjects chapter | translation-oriented + personal experiences | do not repost the notes to avoid unnecessary troubles. thank you |

/* + --------------------------------------------------------------------------------- + | = This article is Haohappy read < > | = Notes in the middle Classes and Objects chapter | = Translation + personal experience | = do not repost to avoid unnecessary troubles, thank you | = Welcome to criticize and correct, hope to make progress together with all PHP enthusiasts! + Category + */section 7-static members of the static member class of the 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, except for a fixed class and restricted access mode, 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. // for example, section 4-examples of constructor and Destructor (see the previous article, you can understand the difference between the // static method and the common method. 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
N "); // print (" Version used: ". Counter: VERSION ."
N ");?>

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.