_unset()這是如何了

來源:互聯網
上載者:User
__unset()這是怎麼了
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.'
';
}
}
}
private function __get($proName){
if($proName == 'age'){
echo ($this->$proName-20).'
';
}
}
private function __isset($proName){
if(isset($this->$proName)){
echo $this->$proName.'存在
';
return;
}
echo $proName.'不存在
';
}
private function __unset($proName){
unset($this->$proName);
}
function say(){
echo 'name........'.$this->name.'|age.........'.$this->age.'|sex..........'.$this->sex.'
';
}
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();


------解決方案--------------------
全部的魔術方法都必須是全域的(public)
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.