PHP预定义接口使用学习笔记

我们知道php提供了6个迭代器接口了,那么这6个接口怎么样呢?有没有朋友都了解?如果各位朋友不知道的可以和小编一起来看看.

PHP预定义了6个接口介绍如下:

pse: collapse; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">

Traversable 遍历接口(检测一个类是否可以使用0 2foreach 0�2进行遍历的接口)

Iterator 迭代器接口(可在内部迭代自己的外部迭代器或类的接口)

IteratorAggregate 聚合式迭代器接口(创建外部迭代器的接口)

OuterIterator 迭代器嵌套接口(将一个或多个迭代器包裹在另一个迭代器中)

RecursiveIterator 递归迭代访问接口(提供递归访问功能)

SeekableIterator 可索引迭代访问接口(实现查找功能)

1.Traversable遍历接口

呵呵,其实它不是一个在PHP中可以使用的接口,内部类才可使用,它有一个用途就是检测一个类是否可以遍历.

  1. if($class instanceof Traversable) {
  2. //foreach
  3. }

2.Iterator迭代器接口,接口摘要:

  1. Iterator extends Traversable
  2. {
  3. //返回当前索引游标指向的元素
  4. abstract public mixed current(void)
  5. //返回当前索引游标指向的元素的键名
  6. abstract public scalar key(void)
  7. //移动当前索引游标指向下一元素
  8. abstract public void next(void)
  9. //重置索引游标的指向第一个元素
  10. abstract public void rewind(void)
  11. //判断当前索引游标指向的是否是一个元素,常常在调用 rewind()或 next()使用
  12. abstract public boolean valid(void)
  13. }

以上可以让一个类实现一个基本的迭代功能,如下可以看到迭代的调用顺序:

  1. class myIterator implements Iterator {
  2. private $position = 0 ;
  3. private $array = array(
  4. "firstelement" ,
  5. "secondelement" ,
  6. "lastelement" ,
  7. );
  8. public function __construct () {
  9. $this -> position = 0 ;
  10. }
  11. function rewind () {
  12. var_dump ( __METHOD__ );
  13. $this -> position = 0 ;
  14. }
  15. function current () {
  16. var_dump ( __METHOD__ );
  17. return $this -> array [ $this -> position ];
  18. }
  19. function key () {
  20. var_dump ( __METHOD__ );
  21. return $this -> position ;
  22. }
  23. function next () {
  24. var_dump ( __METHOD__ );
  25. ++ $this -> position ;
  26. }
  27. function valid () {
  28. var_dump ( __METHOD__ );
  29. return isset( $this -> array [ $this -> position ]);
  30. } //开源软件:phpfensi.com
  31. }
  32. $it = new myIterator ;
  33. foreach( $it as $key => $value ) {
  34. var_dump ( $key , $value );
  35. echo "\n" ;
  36. }

3.IteratorAggregate聚合式迭代器接口,接口摘要:

  1. IteratorAggregate extends Traversable {
  2. //获取外部迭代器
  3. abstract public Traversable getIterator ( void )
  4. }

getIterator是一个Iterator或Traversable接口的类的一个实例,如下获取外部迭代器实现迭代访问.

  1. class myData implements IteratorAggregate {
  2. public $property1 = "Public property one" ;
  3. public $property2 = "Public property two" ;
  4. public $property3 = "Public property three" ;
  5. public function __construct () {
  6. $this -> property4 = "last property" ;
  7. }
  8. public function getIterator () {
  9. return new ArrayIterator ( $this );
  10. }
  11. }
  12. $obj = new myData ;
  13. foreach( $obj as $key => $value ) {
  14. var_dump ( $key , $value );
  15. echo "\n" ;
  16. }

4.ArrayAccess数组式访问接口,接口摘要:

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

如下可像访问数组一样访问对象:

  1. class obj implements arrayaccess {
  2. private $container = array();
  3. public function __construct () {
  4. $this -> container = array(
  5. "one" => 1 ,
  6. "two" => 2 ,
  7. "three" => 3 ,
  8. );
  9. }
  10. public function offsetSet ( $offset , $value ) {
  11. if ( is_null ( $offset )) {
  12. $this -> container [] = $value ;
  13. } else {
  14. $this -> container [ $offset ] = $value ;
  15. }
  16. }
  17. public function offsetExists ( $offset ) {
  18. return isset( $this -> container [ $offset ]);
  19. }
  20. public function offsetUnset ( $offset ) {
  21. unset( $this -> container [ $offset ]);
  22. }
  23. public function offsetGet ( $offset ) {
  24. return isset( $this -> container [ $offset ]) ? $this -> container [ $offset ] : null ;
  25. }
  26. }
  27. $obj = new obj ;
  28. var_dump (isset( $obj [ "two" ]));
  29. var_dump ( $obj [ "two" ]);
  30. unset( $obj [ "two" ]);
  31. var_dump (isset( $obj [ "two" ]));
  32. $obj [ "two" ] = "A value" ;
  33. var_dump ( $obj [ "two" ]);
  34. $obj [] = 'Append 1' ;
  35. $obj [] = 'Append 2' ;
  36. $obj [] = 'Append 3' ;
  37. print_r ( $obj );

5.Serializable序列化接口,接口摘要:

  1. Serializable {
  2. /* 方法 */
  3. abstract public string serialize ( void ) //对象的字符串表示
  4. abstract public mixed unserialize ( string $serialized ) // 构造对象
  5. }

实现该接口的类不再支持__sleep()和__wakeup(),使用很简单,只要序列化对象时serialize方法会被调用,当反序列化时,unserialize方法被调用.

  1. class obj implements Serializable {
  2. private $data ;
  3. public function __construct () {
  4. $this -> data = "My private data" ;
  5. }
  6. public function serialize () {
  7. return serialize ( $this -> data );
  8. }
  9. public function unserialize ( $data ) {
  10. $this -> data = unserialize ( $data );
  11. }
  12. public function getData () {
  13. return $this -> data ;
  14. }
  15. }
  16. $obj = new obj ;
  17. $ser = serialize ( $obj );
  18. print_r($ser);
  19. $newobj = unserialize ( $ser );
  20. print_r($newobj);

6.Closure,接口摘要:

  1. Closure {
  2. /* 方法 */
  3. __construct ( void ) //用于禁止实例化的构造函数
  4. public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] ) //复制一个闭包,绑定指定的$this对象和类作用域。
  5. public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] ) //复制当前闭包对象,绑定指定的$this对象和类作用域。
  6. }
  7. class A {
  8. private static $sfoo = 1 ;
  9. private $ifoo = 2 ;
  10. }
  11. $cl1 = static function() {
  12. return A :: $sfoo ;
  13. };
  14. $cl2 = function() {
  15. return $this -> ifoo ;
  16. };
  17. $bcl1 = Closure :: bind ( $cl1 , null , 'A' );
  18. $bcl2 = Closure :: bind ( $cl2 , new A (), 'A' );
  19. echo $bcl1 (), "\n" ;
  20. echo $bcl2 (), "\n" ;