欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP的攔截器實(shí)例分析

 更新時間:2014年11月03日 11:36:24   投稿:shichen2014  
這篇文章主要介紹了PHP的攔截器,以實(shí)例形式分析了常見的各類攔截器的用法,非常具有實(shí)用價值,需要的朋友可以參考下

本文實(shí)例講述了PHP的攔截器用法。分享給大家供大家參考。具體如下:

PHP提供了幾個攔截器,用于在訪問未定義的方法和屬性時被調(diào)用,如下所示:

1、__get($property)
功能:訪問未定義的屬性是被調(diào)用

2、__set($property, $value)
功能:給未定義的屬性設(shè)置值時被調(diào)用

3、__isset($property)
功能:對未定義的屬性調(diào)用isset()時被調(diào)用

4、__unset($property)
功能:對未定義的屬性調(diào)用unset()時被調(diào)用

5、__call($method, $arg_array)
功能:調(diào)用未定義的方法時被調(diào)用

下面將通過一個小程序來說明這些攔截器的用途:

復(fù)制代碼 代碼如下:
class intercept_demo{
    private $xingming = "";
    private $age = 10;
  
    // 若訪問一個未定義的屬性,則將調(diào)用get{$property}對應(yīng)的方法
    function __get($property){
        $method = "get{$property}";
        if (method_exists($this, $method)){
            return $this->$method();
        }
    }

    // 若給一個未定義的屬性設(shè)置值,則將調(diào)用set{$property}對應(yīng)的方法
    function __set($property, $value){
        $method = "set{$property}";
        if (method_exists($this, $method)){
            return $this->$method($value);
        }  
    }
  
    // 若用戶對未定義的屬性調(diào)用isset方法,
    function __isset($property){
        $method = "isset{$property}";
        if (method_exists($this, $method)){
            return $this->$method();
        }
    }
  
    // 若用戶對未定義的屬性調(diào)用unset方法,
    // 則認(rèn)為調(diào)用對應(yīng)的unset{$property}方法
    function __unset($property){
        $method = "unset{$property}";
        if (method_exists($this, $method)){
            return $this->$method();
        }
    }
  
    function __call($method, $arg_array){
        if (substr($method,0,3)=="get"){
            $property = substr($method,3);
            $property = strtolower(substr($property,0,1)).substr($property,1);
            return $this->$property;
        }
    }
  
    function testIsset(){
        return isset($this->Name);
    }
  
    function getName(){
        return $this->xingming;
    }
  
    function setName($value){
        $this->xingming = $value;
    }
  
    function issetName(){
        return !is_null($this->xingming);
    }
  
    function unsetName(){
        $this->xingming = NULL;
    }
}

$intercept = new intercept_demo();
echo "設(shè)置屬性Name為Li";
$intercept->Name = "Li";
echo "\$intercept->Name={$intercept->Name}";
echo "isset(Name)={$intercept->testIsset()}";
echo "";
echo "清空屬性Name值";
unset($intercept->Name);
echo "\$intercept->Name={$intercept->Name}";
echo "";
echo "調(diào)用未定義的getAge函數(shù)";
echo "age={$intercept->getAge()}";

希望本文所述對大家的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論