Use cases of PHP static and non-static methods

Source: Internet
Author: User
Use cases of PHP static and non-static methods

Static Member

Class members are static members. you can directly use the class name + static member name to access this static member. because the static member exists in the memory, non-static members need to be instantiated to allocate the memory, therefore, static methods cannot access non-static members. because static members exist in the memory, non-static methods can directly merge static members in the class.

Non-static members

All non-Static members are non-Static members. after the class is instantiated, it can be accessed by the instantiated class name .. the lifetime of a non-static member is determined by the lifetime of the object .. static members do not have the concept of survival, because static members always reside in the content.

Class user {private static $ count = 0; public function _ construct () {self ::$ count = self ::$ count + 1;} public function getCount () {return self ::$ count;} public function _ destruct () {self ::$ count = self ::$ count-1 ;}} $ user1 = new user (); $ user2 = new user (); $ user3 = new user (); echo $ user1-> getCount (); echo ""; unset ($ user3); echo $ user1-> getCount ();

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.