魔术方法__get和__set

一、基本概念
__get:当获取对象的受保护属性(protected,private)或者不存在的属性时,系统会调用该方法
__set:当设置对象的受保护属性或者不存在的属性时,系统会调用该方法

二、举例

Class monkey{

Public $name;
Public $food;

Public function __construct( n a m e , name, food)
{
t h i s > n a m e = this->name = name;
t h i s > f o o d = this->food= food;

}
Public function __get(KaTeX parse error: Expected '}', got 'EOF' at end of input: …roperty_exists(proname)){
Return t h i s > this-> proname;
}else
}

}

$monkey = new monkey(泼猴,香蕉);

$monkey->food;//这里调用该保护属性时,就会自动取出发monkey对象中的__get()方法

发布了91 篇原创文章 · 获赞 5 · 访问量 5435

猜你喜欢

转载自blog.csdn.net/weixin_43947156/article/details/104368941
今日推荐