PHP的各种拦截器

来源:互联网 发布:网络监控技术方案 编辑:程序博客网 时间:2024/05/16 08:10

个人测试及用例,主要目的用于外部访问类内部属性的特定方法。

<?phpclass getsetcall{    private $var;    /**     * PHP interceptor     */    public function __set($key,$value)    {        return $this->$key = $value;    }    public function __get($key){        if(isset($key)){            return $this->$key;        }else{            return NULL;        }    }    public function __call($method_name,$arg_array)    {        echo "Your call function is $method_name";        print_r($arg_array);        echo "not exist.";    }    public  function __unset($property) {        echo "Called when outer call private var<br>";        unset($this->$property);    }    public function __isset($property)    {        echo "Called when outer call private var<br>";        return isset($this->$property);    }}/*#======= test __set and __get =======$obj = new getsetcall();$obj->var = "maxoi";echo $obj->var;*//*#======= test __call =======$o = new getsetcall();$o->test(2,"test");*//*#======= test __unset =======$obj = new getsetcall();$obj->var = "a";echo $obj->var;unset($obj->var);echo "Unset after.....".$obj->var;*//*#======= test __isset =======$obj = new getsetcall();$obj->var = "a";echo isset($obj->var);*/?>


0 0
原创粉丝点击