攔截器__get(),__set()

<?php
    class Test
    {
        public $username;
        public $age;
        private $arr = array();

        public function __construct($username,$age)
        {
            $this->username = $username;
            $this->age = $age;
            echo 'username='.$this->username.'<br/>';
            echo 'age='.$this->age.'<br/>';
        }

        public function __get($key)
        {           
            if (isset($this->$key)) 
            {
                return $this->$key;
            }
        }

        public function __set($key,$val)
        {
            $this->arr[$key] = $val;
        }
    }

    $test = new Test('小紅',8);
    echo 'get_name='.$test->name;
    echo '<br/>';
    
    $test->qq = '12345';
    echo 'set_arr["qq"]='.$test->arr['qq'];    
?>
相關文章
相關標籤/搜索