Classes and objects: Member properties, member methods-Learning Note 3

Source: Internet
Author: User

Class Member

Access control for class members
The access control of a class member property or member method in PHP5 is implemented by adding the keyword public, protected (protected), or private (private) before it, and the member method is set to public by default if these keywords are not set.

The following rules apply to the access control permissions of the class:
· Class members defined by public can be accessed from anywhere.
· Class members defined by protected can be accessed by subclasses and parent classes of other classes.
· A class member defined by private can only be accessed by its own class.

Member properties

A variable defined in a class is called a member property (also known as a member variable) and can describe the static or structural characteristics of a class. When a class is instantiated, you can use the operator, "-" to invoke the member properties in the object. The syntax format for calling member properties is as follows:

Member Methods

When a function is defined in a class, it is called a member method, which is used to describe the behavior or dynamic characteristics of the class. In PHP, a member method has the same naming convention as a php normal function, but a member method with the same name is not allowed. After instantiating a class object, you can use the "-a" operator to invoke the member method of the class. The syntax format for calling member methods is:

$this keywords

You can use the keyword $this if you need to access its own member variables and member methods inside the class definition. The keyword can only be used inside a class to get the value of a member variable in the class or to call a member method in that class. The $this syntax format is:


PS: When designing a class, you typically define a data member in a class as a private type, and if you want to use the data outside of the class, you can provide a member method of the public type, which returns the value of the variable.

Classes and objects: Member properties, member methods-Learning Note 3

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