In PHP, public is an access modifier, used to limit the accessibility of modified members. It means "public", that is, it can be accessed in all locations. The syntax is "class Class name {public property or method definition}".
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
What is the usage of public in php
Access control modifiers
形式:
class 类名{
访问控制修饰符 属性或方法定义;
}
Copy after login
There are 3 access modifiers:
- ##public Public: accessible (used) everywhere.
- protected Protected: can only be accessed (used) within the class and in subclasses or parent classes of the class.
- private: can only be accessed (used) within the class.
Their function is to "limit" the "accessibility" of the members they modify;
Accessibility:
It is the "validity" (legality) of using such two syntax forms in the code:
Object->Instance properties or methods;
Class:: Static properties or methods;
The access control modifier needs to be used in conjunction with the location of this syntax form to determine whether it is accessible.
There are 3 access locations (scopes):
Inside a certain class:
Inside a class that inherits a certain class:
A certain Outside the class:
Then, their combined accessibility is as follows:
Summary:
1, public modified Members can be accessed anywhere;
2, inside the class, members at any level can be accessed;
3, public has the broadest accessibility; private has the narrowest accessibility Sexual; protected is in the middle;
Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of What is the usage of public in php. For more information, please follow other related articles on the PHP Chinese website!