Why this cannot be used in a static method

Source: Internet
Author: User

The static method inside the class is not able to access the non-static members of the class, for the simple reason that we want to access the other members of this class in the methods of this class, we need to use $this this reference, and $this This reference pointer is the object that calls this method, we say that the static method is not called by the object, Instead of using the class name to access, so there is no object exists, there is no $this this reference, no $this this reference can not access the non-static members of the class, and because the static members of the class can be accessed without objects, so the static method inside the class can only access the static properties of the class, Since $this does not exist, visiting other static members in static methods we use a special class "Self", and self is similar to $this, except that self is the class that represents this static method. In a static method, you can use the class name of the class in which the method resides, or you can use "self" to access other static members, and if there is no special case, we usually use the latter, the "Self:: member property".

Why this cannot be used in a static method

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.