日期:2014-05-17 浏览次数:20533 次
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.'<br />';
}
}
}
private function __get($proName){
if($proName == 'age'){
echo ($this->$proName-20).'<br />';
}
}
private function __isset($proName){
if(isset($this->$proName)){
echo $this->$proName.'存在<br />';
return;
}
echo $proName.'不存在<br />';
}
private function __unset($proName){
unset($this->$proName);
}
function say(){
echo 'name........'.$this->name.'|age.........'.$this->age.'|sex..........'.$this->sex.'<br />';
}
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();