Static property and method:: Self $this difference

Source: Internet
Author: User

1. Declaring a class property or method as static can be accessed directly without instantiating the class. Static properties cannot be accessed through an object that a class has instantiated (but static methods can).

2. Because static methods do not need to be called through objects, pseudo-variables $this are not available in static methods.

3. Static properties cannot be accessed by the object by operator.

4. Invoking a non-static method in a static manner results E_STRICT in a level of error.

5. Just like all other PHP static variables, static properties can only be initialized to literals or constants, and expressions cannot be used. You can initialize a static property to an integer or an array, but you cannot initialize it to another variable or function return value, or point to an object.

Accessing static variables is similar to accessing a class constant, either by using the class name (from inside or outside the class) or by using the keyword self, followed by the scope-resolution operator (::) and the static variable name starting with $.

::--Self $this differences:

If the referenced variable or method is declared as const or static, then the operator must be used::.

If the referenced variable or method is not declared as const or static, then you must use the operator---.

If you access a const or static variable or method from within a class, you must use self-referencing.

If accessing from within a class is not a const or static variable or method, you must use the self-referencing variable $this.

Static property and method:: Self $this difference

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.