Introduction to static methods and static properties in PHP

Source: Internet
Author: User
This article brings to you the content is about the static method and static properties of the introduction of PHP, there is a certain reference value, the need for friends can refer to, I hope to help you.

Static is divided into two parts: static properties and Static methods

Static things are for class (including class constants), non-static is for the object

Static properties

When defining a property, a property that uses the static modifier of the keyword is called a static property.

Static methods

Methods that are decorated with the static keyword are called static methods.

Can solve the problem with static, no ordinary method to solve the problem (static things can directly use the class access, do not need to instantiate through the object, thus saving memory space, improve efficiency).

Special attention:

    1. In the process of writing code, strictly follow the case specification

    2. Strictly follow: Static content to the class access, non-static is to the object access, do not cross-boundary operation

Third, the difference between self and $this

1. Self represents class, $this represents object

2. Self accesses constant and static properties and static methods inside the class, $this accessing the object's properties and common methods

3, self must cooperate with the scope resolution operator (::) to take effect, $this represents the object, the object is a data type, so $this can be printed separately.

4, can use the place of $this, must be able to using self, but can use self of place not necessarily can use $this

5. Non-static properties and methods are accessed using the object ($this), and static methods and properties or constants are accessed using the Class (self)

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.