PHP developers must master the 6 key word _ Basics

Source: Internet
Author: User

Any PHP developer who uses object-oriented building Web applications needs to master 6 keywords, respectively:

1.Private
2.Public
3.Protected
4.Static
5.Final
6.Abstract

For the first three keywords, they access relationships within the class as follows:


Private

As shown in the figure above, private is the core of access control, so a property (variable) or method that is defined as private in a class can only be accessed within that class, and no instance (object) or subclass of the class can be accessed, nor can you access it directly through the class name.

Protected

The protected access level is second only to private, and the attribute (variable) or method defined as protected can be accessed not only in this class, but also in subclasses of that class, which is not possible with the private property.

Public

Public has the greatest access rights, attributes (variables) or methods that are defined as public can be accessed at any point in the program, at any time.

Static

When we declare a property (variable) as static in a class, the value of the property is visible in all its objects and is a shared variable, so the static property value depends on the class rather than the object. Static properties cannot be accessed through objects, but rather by using the class name plus:: Symbol direct access.
Similarly, static methods have object-sharing attributes, but you need to note the following two points:

1. Directly through the class name plus:: Access static method
2. The $this keyword cannot be used in static methods

Final

If the property (variable) is final decorated, then the property (variable) value cannot be changed and, if it is a function, the function cannot be overwritten or overridden.

Abstract

Classes that are defined as abstract cannot be instantiated. Any class, if at least one of the methods inside it is declared abstract, then the class must be declared abstract. The method defined as abstract simply declares its invocation mode (parameters) and cannot define its specific function implementation.

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.