<?phpclass Person{
private $name;
private $sex;
private $age;
//构造方法
function __construct($name="",$sex="男",$age=1){
$this ->name=$name;
$this ->sex=$sex;
$this->age=$age;
}
//添加__get()方法
public function __get($propertName){
if($propertName=="sex"){
return "保密";
}
else if($propertyName=="age")
{
if($this->age>30)
return $this->age-10;
else
return $this->$propertyName;
}
else
{
return $this->$propertyName;
}
}
}$person1=new Person("张三","男",20);echo "姓名:".$person1->name."<br>";
echo "性别:".$person1->sex."<br>";
echo "年龄:".$person1->age."<br>";
?>我什么我这里返回不了姓名和年龄?
返回的是下面:姓名:
性别:保密
年龄:
请高手解答下 谢谢!
private $name;
private $sex;
private $age;
//构造方法
function __construct($name="",$sex="男",$age=1){
$this ->name=$name;
$this ->sex=$sex;
$this->age=$age;
}
//添加__get()方法
public function __get($propertName){
if($propertName=="sex"){
return "保密";
}
else if($propertyName=="age")
{
if($this->age>30)
return $this->age-10;
else
return $this->$propertyName;
}
else
{
return $this->$propertyName;
}
}
}$person1=new Person("张三","男",20);echo "姓名:".$person1->name."<br>";
echo "性别:".$person1->sex."<br>";
echo "年龄:".$person1->age."<br>";
?>我什么我这里返回不了姓名和年龄?
返回的是下面:姓名:
性别:保密
年龄:
请高手解答下 谢谢!
Cannot access empty property
无法访问空属性把 $this->$propertyName
改为 $this->$propertName