PHP的ArrayAccess接口详解

数组式访问接口,该接口的作用是提供像访问数组一样访问对象的能力

  1. ArrayAccess {  
  2.     // 获取一个偏移位置的值  
  3.     abstract public mixed offsetGet ( mixed $offset )  
  4.     // 设置一个偏移位置的值  
  5.     abstract public void offsetSet ( mixed $offset , mixed $value )  
  6.     // 检查一个偏移位置是否存在  
  7.     abstract public boolean offsetExists ( mixed $offset )  
  8.     // 复位一个偏移位置的值  
  9.     abstract public void offsetUnset ( mixed $offset )  
  1. <?php  
  2. /** 
  3. * ArrayAndObjectAccess 
  4. * 该类允许以数组或对象的方式进行访问 
  5. * 
  6. * @author 疯狂老司机 
  7. */  
  8. class ArrayAndObjectAccess implements ArrayAccess {  
  9.   
  10.     /** 
  11.      * 定义一个数组用于保存数据 
  12.      * 
  13.      * @access private 
  14.      * @var array 
  15.      */  
  16.     private $data = [];  
  17.   
  18.     /** 
  19.      * 以对象方式访问数组中的数据 
  20.      * 
  21.      * @access public 
  22.      * @param string 数组元素键名 
  23.      */  
  24.     public function __get($key) {  
  25.         return $this->data[$key];  
  26.     }  
  27.   
  28.     /** 
  29.      * 以对象方式添加一个数组元素 
  30.      * 
  31.      * @access public  
  32.      * @param string 数组元素键名 
  33.      * @param mixed  数组元素值 
  34.      * @return mixed 
  35.      */  
  36.     public function __set($key,$value) {  
  37.         $this->data[$key] = $value;  
  38.     }  
  39.   
  40.     /** 
  41.      * 以对象方式判断数组元素是否设置 
  42.      * 
  43.      * @access public 
  44.      * @param 数组元素键名 
  45.      * @return boolean 
  46.      */  
  47.     public function __isset($key) {  
  48.         return isset($this->data[$key]);  
  49.     }  
  50.   
  51.     /** 
  52.      * 以对象方式删除一个数组元素 
  53.      * 
  54.      * @access public 
  55.      * @param 数组元素键名 
  56.      */  
  57.     public function __unset($key) {  
  58.         unset($this->data[$key]);  
  59.     }  
  60.   
  61.     /** 
  62.      * 以数组方式向data数组添加一个元素 
  63.      * 
  64.      * @access public 
  65.      * @abstracting ArrayAccess 
  66.      * @param string 偏移位置 
  67.      * @param mixed  元素值 
  68.      */  
  69.     public function offsetSet($offset,$value) {  
  70.         if (is_null($offset)) {  
  71.             $this->data[] = $value;  
  72.         } else {  
  73.             $this->data[$offset] = $value;  
  74.         }  
  75.     }  
  76.   
  77.     /** 
  78.      * 以数组方式获取data数组指定位置元素 
  79.      * 
  80.      * @access public    
  81.      * @abstracting ArrayAccess        
  82.      * @param 偏移位置 
  83.      * @return mixed 
  84.      */  
  85.     public function offsetGet($offset) {  
  86.         return $this->offsetExists($offset) ? $this->data[$offset] : null;  
  87.     }  
  88.   
  89.     /** 
  90.      * 以数组方式判断偏移位置元素是否设置 
  91.      * 
  92.      * @access public 
  93.      * @abstracting ArrayAccess 
  94.      * @param 偏移位置 
  95.      * @return boolean 
  96.      */  
  97.     public function offsetExists($offset) {  
  98.         return isset($this->data[$offset]);  
  99.     }  
  100.   
  101.     /** 
  102.      * 以数组方式删除data数组指定位置元素 
  103.      * 
  104.      * @access public 
  105.      * @abstracting ArrayAccess      
  106.      * @param 偏移位置 
  107.      */  
  108.     public function offsetUnset($offset) {  
  109.         if ($this->offsetExists($offset)) {  
  110.             unset($this->data[$offset]);  
  111.         }  
  112.     }  
  113.   
  114. }  
  115.   
  116. $animal = new ArrayAndObjectAccess();  
  117.   
  118. $animal->dog = 'dog'// 调用ArrayAndObjectAccess::__set  
  119. $animal['pig'] = 'pig'// 调用ArrayAndObjectAccess::offsetSet  
  120. var_dump(isset($animal->dog)); // 调用ArrayAndObjectAccess::__isset  
  121. var_dump(isset($animal['pig'])); // 调用ArrayAndObjectAccess::offsetExists  
  122. var_dump($animal->pig); // 调用ArrayAndObjectAccess::__get  
  123. var_dump($animal['dog']); // 调用ArrayAndObjectAccess::offsetGet  
  124. unset($animal['dog']); // 调用ArrayAndObjectAccess::offsetUnset  
  125. unset($animal->pig); // 调用ArrayAndObjectAccess::__unset  
  126. var_dump($animal['pig']); // 调用ArrayAndObjectAccess::offsetGet  
  127. var_dump($animal->dog); // 调用ArrayAndObjectAccess::__get  
  128.   
  129. ?>  
以上输出:

boolean true
boolean true
string 'pig' (length=3)
string 'dog' (length=3)
null
null


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值