时间:2021-07-01 10:21:17 帮助过:3人阅读
class Person {
private $name;
private $age;
private $sex;
//2. 构造方法 //作用: 给对象属性初始值
function __construct($name='', $age='0', $sex='男'){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
private function __set($proName, $proValue){
if($proName == 'age'){
if($proValue<0 || $proValue>100){
echo '设置的年龄不合法';
return;
}else{
$this->$proName=$proValue;
echo $this->age.'
';
}
}
}
private function __get($proName){
if($proName == 'age'){
echo ($this->$proName-20).'
';
}
}
private function __isset($proName){
if(isset($this->$proName)){
echo $this->$proName.'存在
';
return;
}
echo $proName.'不存在
';
}
private function __unset($proName){
unset($this->$proName);
}
function say(){
echo 'name........'.$this->name.'|age.........'.$this->age.'|sex..........'.$this->sex.'
';
}
function __destruct(){
}
}
$p1 = new Person('yeteng', 26, '男');
$p2 = new Person('yeben', 28, '男');
$p3 = new Person('wenjing', 40, '女');
$p1->age=99; //自动触发__set
$p1->age; //自动触发__get
isset($p1->name);//自动触发__isset
unset($p1->age); //自动触发__unset
$p1->say();