一、基本概念
__get:当获取对象的受保护属性(protected,private)或者不存在的属性时,系统会调用该方法
__set:当设置对象的受保护属性或者不存在的属性时,系统会调用该方法
二、举例
Class monkey{
Public $name;
Public $food;
Public function __construct(
food)
{
name;
food;
}
Public function __get(KaTeX parse error: Expected '}', got 'EOF' at end of input: …roperty_exists(proname)){
Return
proname;
}else
}
}
$monkey = new monkey(泼猴,香蕉);
$monkey->food;//这里调用该保护属性时,就会自动取出发monkey对象中的__get()方法