PHP Magic Method Setter and Getter
2021-04-24 05:26
标签:optional function magic att start ble cte without AMM PHP Magic Method Setter and Getter 标签:optional function magic att start ble cte without AMM 原文地址:https://www.cnblogs.com/luoxuw/p/12236725.html 30)
{
$current =$this->$property;
//notice: $this->property(without $) will create a new attribute called property, $property
$this->$property= $value;
echo "You are going to update $property from $current to $value"."
";
}else{
echo "Update $property failed!
";
}
}
public function __get($property)
{
return "You are trying to get inaccessible $property 's value: " . $this->$property . "
";
}
}
$kid= new kids; //() is optional, automatically call __constructor(){}
//1. testing protected variable
$kid->height =55; //this will implicitly and automatically call __set() because height property is protected
//You are going to update height from 23 to 55
$kid->height =23; //Update height failed!
echo $kid->height; //You are trying to get inaccessible height 's value: 55
//2. testing private variable
$kid->age = 37; //You are going to update age from to 37
echo $kid->age; //You are trying to get inaccessible age 's value: 37
//3.testing undeclared variable in the class
$kid->weight =40;
/*You are going to update weight from You are trying to get inaccessible weight 's value:
to 40 */ //why ?
//because $current=$this->$property =$this->weight statement invoke the getter
echo $kid->weight; //40
文章标题:PHP Magic Method Setter and Getter
文章链接:http://soscw.com/essay/78795.html