There are three access modifiers in PHP, which are:
public (public, default)
protected (protected)
private (private)
They can be used separately Used on the attributes and methods of a class (the attributes and methods of a class are collectively called members of a class) to modify the access permissions of class members.
public (public, default)
In PHP5, if a class does not specify an access modifier for a member, the default is public access.
/*
The following two methods have the same effect of declaring access permissions
*/
function say(){};
publilc function say(){};
When a member of a class is declared with a public access modifier, the member can be accessed and operated by external code.
private (private)
Members defined as private are visible to all members within the class and have no access restrictions. Access is not allowed outside the class.
protected (protected)
protected is a little more complicated. It is declared as a protected member and only allows access by subclasses of this class.
Access permission status table:
Access rights |
public |
protected |
private |
All |
★ |
||
Subclass |
★ |
★ |
|
Within class |
★ |
★ |
★ |