php中的魔术方法

<?php
    class A {
        public $config = array(
            "name" => "senlin",
        );
        
        // 读取未定义的便利的时候就是调用这个方法。
        public function __get($name){
            if(isset($this->config[$name])) {
                return $this->config[$name];
            }
            return null;
        }
        
        public function f() {
            // 这个时候,由于A中没有 $this->name ,
            // 但是这个时候 定义 __get 就会调用__get. 
            $this->xxx = $this->name;
        }
        
        public function s(){
            echo $this->xxx;
        }
    }
    
    $x = new A();
    $x->f();
    $x->s();
?>

参考链接 http://www.php.net/manual/zh/language.oop5.overloading.php#object.get

posted @ 2013-04-08 11:04  刘牛德  阅读(104)  评论(0)    收藏  举报