When setting private member attributes through _ set ($ propertyName, $ value), use $ this-& amp; gt; $ propertyName $ value can be used to modify the private variable value. The $ this-& amp; gt; propertyName $ value write rule cannot be used to modify the variable value. Complete... when setting private member attributes through _ set ($ propertyName, $ value,
You can use $ this-> $ propertyName = $ value to modify the private variable value,
You cannot modify the variable value by using the write rules $ this-> propertyName = $ value.
Complete code:
Class BasePerson {
private $name; private $age; private $average; function __construct(){ $num_args = func_num_args(); if($num_args == 1){ $this->name = func_get_arg(0); } elseif($num_args == 2){ $this->name = func_get_arg(0); $this->age = func_get_arg(1); } elseif($num_args == 3){ $this->name = func_get_arg(0); $this->age = func_get_arg(1); $this->average = func_get_arg(2); } } private function __set($propertyName,$propertyValue){ echo "
BasePerson __set()"; if($propertyName == "name"){ if($propertyValue == "zhangsan" || $propertyValue == "wangwu"){ $this->$propertyName = $propertyValue; } } elseif($propertyName=="age"){ if($propertyValue >150 || $propertyValue <0){ return ; } } $this->$propertyName = $propertyValue; } function __toString(){ return "
name:".($this->name)."
age:".($this->age)."
average".($this->average); }
}
Reply content:
When the private member attribute is set through _ set ($ propertyName, $ value,
You can use $ this-> $ propertyName = $ value to modify the private variable value,
You cannot modify the variable value by using the write rules $ this-> propertyName = $ value.
Complete code:
Class BasePerson {
private $name; private $age; private $average; function __construct(){ $num_args = func_num_args(); if($num_args == 1){ $this->name = func_get_arg(0); } elseif($num_args == 2){ $this->name = func_get_arg(0); $this->age = func_get_arg(1); } elseif($num_args == 3){ $this->name = func_get_arg(0); $this->age = func_get_arg(1); $this->average = func_get_arg(2); } } private function __set($propertyName,$propertyValue){ echo "
BasePerson __set()"; if($propertyName == "name"){ if($propertyValue == "zhangsan" || $propertyValue == "wangwu"){ $this->$propertyName = $propertyValue; } } elseif($propertyName=="age"){ if($propertyValue >150 || $propertyValue <0){ return ; } } $this->$propertyName = $propertyValue; } function __toString(){ return "
name:".($this->name)."
age:".($this->age)."
average".($this->average); }
}
$ Name = 'test'; $ name = '000000'; var_dump ($ test ); // output 123 // hope you can understand // similarly $ this-> $ proertyName // you need to check the value of $ proertyName, is to set the value of $ proertyName. this attribute $ proertyName = 'age'; $ this-> $ proertyName = '12'; var_dump ($ this-> age ); // output 12 $ this-> proertyName // set the property of proertyName.
Landlord,$this->$propertyName = $valueThis assignment is$thisThe variable in the instance is$propertyNameVariable value.$propertyNameIt is still a variable, and$this->propertyNameIs the $ this attribute;
The former is like this, for example$propertyName = 'enable';
So$this->$propertyNameIt is equivalent$this->enable;