Static variable statics in PHP and Java

Source: Internet
Author: User
Tags php language
Static variable statics in PHP and Java
Because static members are created when the class is first loaded, it is possible to use the class name to access static members that do not require objects outside of the class, and static members are shared by each instance object of the class, so can we use objects in our PHP language to access static members of the class?

Static members do not exist within each object, but each object can be shared, so if we use objects to access members, we will see that there is no such attribute definition, using object access to static members , in other object-oriented languages,

Java is the way that you can access static members by using objects

Static methods inside a class can only access static properties of the class, and static methods within the class cannot access the non-static members of the class, for the simple reason that we want to access the other members of this class in a method of this class, we need to use the $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, but the use of 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 static properties of the class, that is, $this does not exist, and in static methods we use a special class "self"; 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".

  • 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.