Scope of the PHP method

Source: Internet
Author: User

PHP supports 6 method scopes: Public, private, protected, abstract, final, and Static, and this article discusses only the first 5 scopes.

Public: Common scope. (like air and sunshine, the world is public)

The permissions of the public scope are most open, the inside and outside of the class are accessible, the parent and subclass of the class are accessible, the object of the class can be accessed, there is no special permission restrictions, and can be used anywhere.

Private: Privacy scope. (like the emperor's concubine, only for the Emperor "class" himself to enjoy, even the Emperor's brother "class instance object" and son grandson "sub-category" Can not be used)

Private scope declaration methods can only be used inside a class, cannot have an instantiated object call, or be used by subclasses of a class. It is suitable for declaring "helper methods" within a class, which are used primarily to perform some "tactical" logic (the logic of trivial details that are not directly related to the primary logic) and to provide support for the "strategic" logic (the primary logic).

Protected: protected scope. (Just like the emperor's throne, only the emperor himself and his son grandson, can not give the emperor's father, "father, has done the emperor, do not need the throne" and brother "class instance object", outsiders are not want to think about it)

A method that protects the scope declaration can be used only in the class and its subclasses (cannot be used in the parent class, cannot be used outside of the class, cannot be called by an instantiated object), and these methods may be used to help the class or subclass complete internal computations.

Abstract: Abstraction scope. (equivalent to "first Emperor's decree", the emperor is only responsible for assigning and arranging things, but the king of succession is responsible for implementation and implementation. The first emperor appointed these things must be a precondition, the emperor of succession can choose to perform "implementation" or not execute according to the circumstances and needs

Abstract scopes are special, only declared in the parent class, but implemented in subclasses. Only a class that is declared abstract can declare an abstraction method. If you want to define an application programming interface (API), you can use abstract method declarations.

Final: The scope cannot be changed. (equivalent to the emperor's "imperial decree", the emperor's son and subordinates can not be changed, nor can imitate the emperor issued the same "decree". )

The final declared method will prevent the subclass from overwriting this method.

Scope of the PHP 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.