search
Homephp教程php手册PHP魔术方法__GET、__SET使用实例

这篇文章主要介绍了PHP魔术方法__GET、__SET使用实例,本文在代码中做了详细的注释,需要的朋友可以参考下

__get()  - 读取不可访问属性的值时,__get() 会被调用。

__set()  - 在给不可访问属性赋值时,__set() 会被调用。

复制代码 代码如下:


/**
 * 清晰的认识__get() __set()
 */
class Example {
   
    //公有的属性
    public $public = 'pub' ;
    //受保护的 - 子类中该属性可用
    protected $protected = 'pro';
    //私有的 - 只能此类使用此属性
    private $private = 'pri';
   
    //当访问对象中的属性不存在或者非公有属性的时候自动加载__get()方法
    public function __get($name){
        return '调用__get()方法:'.$name;
    }
   
    //当给对象的一个属性赋值的时候如果该属性不存在或者是非公有属性则自动加载__set()方法
    public function __set($name,$value){
        echo "\nname:".$name.',value:'.$value."\n";
    }
}

$example = new Example;
echo '

';<br>
echo $example->public."\n";<br>
echo $example->protected."\n";<br>
echo $example->private."\n";<br>
echo $example->other."\n";<br>
echo '<hr>';<br>
$example->public = 'lic';   //这个赋值成功所有没有显示<br>
$example->protected = 'tec';<br>
$example->private = 'vat';<br>
$example->other = 'er';<br>
echo '<br>';<br>
echo '打印 public 属性:'.$example->public;<br>

<p>结果如下:</p>
<p></p><p><span>复制代码</span> 代码如下:</p><p><br>
pub<br>
调用__get()方法:protected<br>
调用__get()方法:private<br>
调用__get()方法:other</p>
<p>name:protected,value:tec</p>
<p>name:private,value:vat</p>
<p>name:other,value:er</p>
<p>打印 public 属性:lic<br>
</p>
,


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

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),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use