Home > php教程 > php手册 > body text

PHP object-oriented - access modifiers

WBOY
Release: 2016-07-09 09:10:33
Original
1577 people have browsed it

There are three access modifiers in PHP, namely:

  • public (public, default)
  • protected (protected)
  • private ( Private)

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.

public (public, default)

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(){};
Copy after login

When a member of a class is declared with a public access modifier, the member can be accessed and manipulated 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 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

所有

子类

类内

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

To summarize with the following example:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!