There are three access modifiers in PHP, namely:
They can be used on the attributes and methods of a class respectively (the attributes and methods of a class are collectively called members of a class) to modify the access rights of class members.
In PHP5, if a class does not specify an access modifier for a member, the default is public access.
<span style="color: #008000">/*</span><span style="color: #008000"> 以下两个方法声明访问权限效果相同 </span><span style="color: #008000">*/</span> <span style="color: #0000ff">function</span><span style="color: #000000"> say(){}; publilc </span><span style="color: #0000ff">function</span> say(){};
When a member of a class is declared with a public access modifier, the member can be accessed and manipulated by external code.
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 is a little more complicated. Members declared as protected can only be accessed by subclasses of the class.
Access rights status table:
Access
|
public
|
protected |
private |
||||||||||||||||
All <span style="color: #000000">php </span><span style="color: #0000ff">class</span><span style="color: #000000"> Woman{ </span><span style="color: #0000ff">public</span> <span style="color: #800080">$name</span> = 'lisa'; <span style="color: #008000">//</span><span style="color: #008000"> 公共的访问权限</span> <span style="color: #0000ff">protected</span> <span style="color: #800080">$money</span> = 3000.00; <span style="color: #008000">//</span><span style="color: #008000"> 受保护的权限</span> <span style="color: #0000ff">private</span> <span style="color: #800080">$age</span> = 35; <span style="color: #008000">//</span><span style="color: #008000"> 私有的访问权限</span> <span style="color: #0000ff">function</span><span style="color: #000000"> printInfo(){ </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$this</span>-><span style="color: #000000">name; </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$this</span>-><span style="color: #000000">money; </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$this</span>-><span style="color: #000000">age; } </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">function</span><span style="color: #000000"> secret(){ </span><span style="color: #0000ff">echo</span> "这是个秘密!"<span style="color: #000000">; } } </span><span style="color: #800080">$woman</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> Woman(); </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$woman</span>->name; <span style="color: #008000">//</span><span style="color: #008000"> 公共属性可以访问 // echo $woman->money; // 受保护属性,报致命错误 // echo $woman->age; // 私有属性,报致命错误</span> <span style="color: #800080">$woman</span>->printInfo(); <span style="color: #008000">//</span><span style="color: #008000"> 可以打印三个属性的信息,因为printInfo是公共方法 // $woman->secret(); // 私有方法,访问出错</span> <span style="color: #0000ff">class</span> Girl <span style="color: #0000ff">extends</span><span style="color: #000000"> Woman{ </span><span style="color: #008000">//</span><span style="color: #008000"> 可以重新定义父类的public和protected方法,但不能定义private的 // protected $money = 2000.00; // 可以从新定义</span> <span style="color: #0000ff">function</span><span style="color: #000000"> printInfo(){ </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$this</span>-><span style="color: #000000">name; </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$this</span>-><span style="color: #000000">money; </span><span style="color: #008000">//</span><span style="color: #008000"> echo $this->age; // 找不到属性</span> <span style="color: #000000"> } } </span><span style="color: #800080">$girl</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> Girl(); </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$girl</span>->name; <span style="color: #008000">//</span><span style="color: #008000"> 公共属性可以访问 // echo $girl->money; // 受保护属性,报致命错误 // echo $girl->age; // 私有属性,找不到属性</span> <span style="color: #800080">$girl</span>->printInfo(); <span style="color: #008000">//</span><span style="color: #008000"> 显示$name,$money,找不到$age属性;</span> ?> Copy after login |
★ |
|
|
||||||||||||||||
Subclass |
★ |
★ |
|
||||||||||||||||
Within class |
★ |
★ |
★ |