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:
$对象名->成员方法名称([参数1,参数2,……]) |
$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:
$对象名->成员属性名称 $对象名->成员方法名称([参数1,参数2,……]) |
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