search
Homephp教程php手册PHP object-oriented - access modifiers

PHP object-oriented - access modifiers

Jul 09, 2016 am 09:10 AM
mdphpcodemodifierOpen sourceprogrammingprogramming languageaccesssoftware developmentobject-oriented

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

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>
?>

Subclass

Within class

To summarize with the following example:
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor