Home >Backend Development >PHP Tutorial >PHP object-oriented - introduction to access modifiers_PHP tutorial
There are three access modifiers in PHP, namely:
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 rights 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(){};
public 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 slightly more complicated. It is declared as a protected member and only allows access by subclasses of this class.
Access permission status table:
|
public |
protected |
private |
||||||||||||||||
All |
★ |
||||||||||||||||||
Subclass |
★ |
★ |
|||||||||||||||||
Within class |
★ |
★ |
★ |