您的位置:首页 > 编程语言 > PHP开发

PHP预定义接口之 ArrayAccess

2017-02-09 16:52 399 查看
ArrayAccess

  先说 ArrayAccess 吧!ArrayAccess 的作用是使得你的对象可以像数组一样可以被访问。应该说 ArrayAccess 在PHP5中才开始有的,PHP5中加入了很多新的特性,当然也使类的重载也加强了,PHP5 中添加了一系列接口,这些接口和实现的 Class 统称为 SPL。

ArrayAccess 这个接口定义了4个必须要实现的方法:

1 {
2    abstract public offsetExists ($offset)  //检查偏移位置是否存在
3    abstract public offsetGet ($offset)     //获取一个偏移位置的值
4    abstract public void offsetSet ($offset ,$value) //设置一个偏移位置的值
5    abstract public void offsetUnset ($offset)       //复位一个偏移位置的值
6 }


所以我们要使用ArrayAccess这个接口,就要实现相应的方法,这几个方法不是随便写的,我们可以看一下 ArrayAccess 的原型:

1 /**
2  * Interface to provide accessing objects as arrays.
3  * @link http://php.net/manual/en/class.arrayaccess.php 4  */
5 interface ArrayAccess {
6
7     /**
8      * (PHP 5 >= 5.0.0)<br/>
9      * Whether a offset exists
10      * @link http://php.net/manual/en/arrayaccess.offsetexists.php 11      * @param mixed $offset <p>
12      * An offset to check for.
13      * </p>
14      * @return boolean true on success or false on failure.
15      * </p>
16      * <p>
17      * The return value will be casted to boolean if non-boolean was returned.
18      */
19     public function offsetExists($offset);
20
21     /**
22      * (PHP 5 >= 5.0.0)<br/>
23      * Offset to retrieve
24      * @link http://php.net/manual/en/arrayaccess.offsetget.php 25      * @param mixed $offset <p>
26      * The offset to retrieve.
27      * </p>
28      * @return mixed Can return all value types.
29      */
30     public function offsetGet($offset);
31
32     /**
33      * (PHP 5 >= 5.0.0)<br/>
34      * Offset to set
35      * @link http://php.net/manual/en/arrayaccess.offsetset.php 36      * @param mixed $offset <p>
37      * The offset to assign the value to.
38      * </p>
39      * @param mixed $value <p>
40      * The value to set.
41      * </p>
42      * @return void
43      */
44     public function offsetSet($offset, $value);
45
46     /**
47      * (PHP 5 >= 5.0.0)<br/>
48      * Offset to unset
49      * @link http://php.net/manual/en/arrayaccess.offsetunset.php 50      * @param mixed $offset <p>
51      * The offset to unset.
52      * </p>
53      * @return void
54      */
55     public function offsetUnset($offset);
56 }


 下面我们可以写一个例子,非常简单:

1 <?php
2 class Test implements ArrayAccess
3 {
4     private $testData;
5
6     public function offsetExists($key)
7     {
8         return isset($this->testData[$key]);
9     }
10
11     public function offsetSet($key, $value)
12     {
13         $this->testData[$key] = $value;
14     }
15
16     public function offsetGet($key)
17     {
18         return $this->testData[$key];
19     }
20
21     public function offsetUnset($key)
22     {
23         unset($this->testData[$key]);
24     }
25 }
26
27   $obj = new Test();
28
29   //自动调用offsetSet方法
30   $obj['data'] = 'data';
31
32   //自动调用offsetExists
33   if(isset($obj['data'])){
34     echo 'has setting!';
35   }
36   //自动调用offsetGet
37   var_dump($obj['data']);
38
39   //自动调用offsetUnset
40   unset($obj['data']);
41   var_dump($test['data']);
42
43   //输出:
44   //has setting!
45   //data
46   //null


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ArrayAccess php