21,881
社区成员
发帖
与我相关
我的任务
分享
<?php
header("Content-type:text/html;charset=utf-8");
class Person{
private $name;
private $age;
private $sex;
public function __construct($name){
$this->name = $name;
$this->age = "23岁";
$this->sex = "男";
}
public function display(){
echo "姓名:$this->name<br>";
echo "年龄:$this->age<br>";
echo "性别:$this->sex<br>";
}
public function __destruct(){
//echo "$this->name: 正在清理内存...<br>";
}
public function __set($propertyName, $propertyValue){
if ($propertyName == "sex"){
if ($propertyValue == "男" || $propertyValue == "女"){
$this->sex = $propertyValue;
}
else{
die("赋值非法,只能是:女|男");
}
}
}
public function __get($propertyName){
if($propertyName == "sex"){
return "保密";
}
else if($propertyName == "age"){
return $this->age;
}
else if($propertyName == "name"){
return $this->name;
}
}
public function __isset($propertyName){
if($propertyName == "name"){
return false;
}
else{
return isset($this->$propertyName);
}
}
public function __unset($propertyName){
if($propertyName == "name"){
return false;
}
else{
unset($this->$propertyName);
}
}
}
$per1 = new Person("Gibi");
$per1->display();
unset($per1->sex);
$per1->display();
?>