当前位置:首页 > PHP教程 > php面向对象 > 列表

PHP之预定义接口详解

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-14 15:13:33 浏览: 评论:0 

这篇文章主要整理了PHP之预定义接口,在平时项目过程中比较常用的四个接口:IteratorAggregate(聚合式aggregate迭代器Iterator)、Countable、ArrayAccess、Iterator,需要的朋友可以参考下

在PHP中有好几个预定义的接口,比较常用的四个接口(IteratorAggregate(聚合式aggregate迭代器Iterator)、Countable、ArrayAccess、Iterator)分别给大家详细介绍下。

IteratorAggregate(聚合式aggregate迭代器Iterator)接口,代码如下:

  1. IteratorAggregate extends Traversable { 
  2.  abstract public Traversable getIterator(void) 
  3. } 

这个接口实现了一个功能——创建外部迭代器,具体怎么理解呢,当我们使用foreach对对象进行便遍历的时候,如果没有继承IteratorAggregate接口,遍历的是对象中所有的public属性(只能是public $var这种形式)。要是继承了IteratorAggregate,会使用类中实现的getIterator方法返回的对象,这里要注意返回的一定要是一个Traversable对象或者扩展自Traversable的对象,否则会抛出异常:

  1. //看个例子 
  2. class My{ 
  3.  private $_data = [ 
  4.  'a' => '燕睿涛', 
  5.  'b' => 'yanruitao', 
  6.  'c' => 'LULU', 
  7.  ]; 
  8.    
  9.  public function getIterator() 
  10.  { 
  11.  return new ArrayIterator($this->_data); 
  12.  } 
  13. } 
  14. $obj = new My; 
  15. foreach ($obj as $key => $value) { 
  16.  echo "$key => $value\n"; 
  17. } 
  18. //输出结果为空  
  19.  
  20. class My implements IteratorAggregate { 
  21.  private $_data = [ 
  22.  'a' => '燕睿涛', 
  23.  'b' => 'yanruitao', 
  24.  'c' => 'LULU', 
  25.  ]; 
  26.  
  27.  public function getIterator() 
  28.  { 
  29.  return new ArrayIterator($this->_data); 
  30.  } 
  31. } 
  32. $obj = new My; 
  33. foreach ($obj as $key => $value) { 
  34.  echo "$key => $value\n"; 
  35. } 
  36. //结果: 
  37. a => 燕睿涛 
  38. b => yanruitao 
  39. c => LULU 

Countable接口,代码如下:

  1. Countable { 
  2.  abstract public int count(void) 
  3. } 

这个接口用于统计对象的数量,具体怎么理解呢,当我们对一个对象调用count的时候,如果函数没有继承Countable将一直返回1,如果继承了Countable会返回所实现的count方法所返回的数字,看看下面的例子:

  1. class CountMe 
  2. {  
  3.  protected $_myCount = 3;  
  4.  
  5.  public function count()  
  6.  {  
  7.  return $this->_myCount;  
  8.  }  
  9. }  
  10.  
  11. $countable = new CountMe();  
  12. echo count($countable); 
  13. //返回1 
  14.  
  15. class CountMe implements Countable 
  16. {  
  17.  protected $_myCount = 3;  
  18.  
  19.  public function count()  
  20.  {  
  21.  return $this->_myCount;  
  22.  }  
  23. }  
  24.  
  25. $countable = new CountMe();  
  26. echo count($countable);  
  27. //返回3 

ArrayAccess接口:

  1. ArrayAccess { 
  2.  abstract public boolean offsetExists(mixed $offset) 
  3.  abstract public mixed offsetGet(mixed $offset) 
  4.  public void offsetSet(mixed $offset, mixed $value) 
  5.  public void offsetUnset(mixed $offset) 
  6. } 
  7.  
  8.  
  9. class CountMe 
  10. {  
  11.  protected $_myCount = 3;  
  12.  
  13.  public function count()  
  14.  {  
  15.   return $this->_myCount;  
  16.  }  
  17. }  
  18.  
  19. $countable = new CountMe();  
  20. echo count($countable); 
  21. //返回1 
  22.  
  23. class CountMe implements Countable 
  24. {  
  25.  protected $_myCount = 3;  
  26.  
  27.  public function count()  
  28.  {  
  29.   return $this->_myCount;  
  30.  }  
  31. }  
  32.  
  33. $countable = new CountMe();  
  34. echo count($countable);  
  35. //返回3 

ArrayAccess接口,代码如下:

  1. ArrayAccess { 
  2.  abstract public boolean offsetExists(mixed $offset) 
  3.     abstract public mixed offsetGet(mixed $offset) 
  4.     public void offsetSet(mixed $offset, mixed $value) 
  5.     public void offsetUnset(mixed $offset) 
  6. } 

这个接口的作用是让我们可以像访问数组一样访问对象,这个怎么说好呢,我猜其实就是php在词法分析的时候如果碰到了数组的方式使用对象,就回去对象中查找是否有实现ArrayAccess如果有的话,进行对应的操作(set、unset、isset、get),这样我们就可以在类里面放置一个array,让类实现数组方式的基本操作,下面看个例子:

  1. class myObj 
  2. { 
  3.    
  4. } 
  5. $obj = new myObj; 
  6. $obj['name']; 
  7. //Fatal error: Cannot use object of type myObj as array in  
  8.  
  9. class myObj implements ArrayAccess  
  10. { 
  11.  public function offsetSet($offset, $value) 
  12.  { 
  13.   echo "offsetSet : {$offset} => {$value}\n"; 
  14.  } 
  15.  
  16.  public function offsetExists($offset) 
  17.  { 
  18.   echo "offsetExists : {$offset}\n"; 
  19.  } 
  20.  
  21.  public function offsetUnset($offset) 
  22.  { 
  23.   echo "offsetUnset : {$offset}\n"; 
  24.  } 
  25.  
  26.  public function offsetGet($offset) 
  27.  { 
  28.   echo "offsetGet : {$offset}\n"; 
  29.  } 
  30. } 
  31. $obj = new myObj; 
  32. $obj[1] = '燕睿涛'; 
  33. isset($obj['name']); 
  34. unset($obj['name']); 
  35. $obj['yrt']; 
  36.  
  37. //输出结果: 
  38. offsetSet : 1 => 燕睿涛 
  39. offsetExists : name 
  40. offsetUnset : name 
  41. offsetGet : yrt 
  42.  
  43. class myObj implements ArrayAccess  
  44. { 
  45.  private $_data = []; 
  46.  public function offsetSet($offset, $value) 
  47.  { 
  48.   $this->_data[$offset] = $value; 
  49.  } 
  50.  
  51.  public function offsetExists($offset) 
  52.  { 
  53.   return isset($this->_data[$offset]); 
  54.  } 
  55.  
  56.  public function offsetUnset($offset) 
  57.  { 
  58.   unset($this->_data[$offset]); 
  59.  } 
  60.  
  61.  public function offsetGet($offset) 
  62.  { 
  63.   return $this->_data[$offset]; 
  64.  } 
  65. } 
  66.  
  67. $obj = new myObj; 
  68. $obj['yrt'] = '燕睿涛'; 
  69. var_dump($obj['yrt']); 
  70. var_dump(isset($obj['yrt'])); 
  71. unset($obj['yrt']); 
  72. var_dump(isset($obj['yrt'])); 
  73. var_dump($obj['yrt']); 
  74.  
  75. //输出: 
  76. string(9) "燕睿涛" 
  77. bool(true) 
  78. bool(false) 
  79. Notice: Undefined index: yrt //最后一个会报出Notice 

上面的对象只能是基本的数组操作,连遍历都不行,结合之前的IteratorAggregate可以进行foreach:

  1. class myObj implements ArrayAccess, IteratorAggregate 
  2. { 
  3. private $_data = []; 
  4.  
  5.  public function getIterator() 
  6.  { 
  7.   return new ArrayIterator($this->_data); 
  8.  } 
  9.  
  10.  ...... 
  11. } 
  12. $obj = new myObj; 
  13. $obj['yrt'] = '燕睿涛'; 
  14. $obj[1] = '燕睿涛'; 
  15. $obj['name'] = '燕睿涛'; 
  16. $obj['age'] = 23; 
  17.  
  18. foreach ($obj as $key => $value) { 
  19.  echo "{$key} => {$value}\n"; 
  20. } 
  21. //输出: 
  22. yrt => 燕睿涛 
  23. 1 => 燕睿涛 
  24. name => 燕睿涛 
  25. age => 23 

Iterator接口:

  1. Iterator extends Traversable { 
  2.     abstract public mixed current(void) 
  3.     abstract public scalar key(void) 
  4.     abstract public void next(void) 
  5.     abstract public void rewind(void) 
  6.     abstract public boolean valid(void) 
  7. } 

可在内部迭代自己的外部迭代器或类的接口,这是官方文档给出的解释,看着还是不好理解,其实我感觉这个接口实现的功能和trratorAggregate(文档:创建外部迭代器接口,接口直接返回一个迭代器)类似,不过这个在类的定义里面自己实现了,看个例子:

  1. class myObj implements Iterator{ 
  2.  
  3.  private $_data = []; 
  4.  
  5.  public function __construct(Array $arr) 
  6.  { 
  7.  $this->_data = $arr; 
  8.  } 
  9.  
  10.  public function current() 
  11.  { 
  12.  return current($this->_data); 
  13.  } 
  14.  
  15.  public function key() 
  16.  { 
  17.  return key($this->_data); 
  18.  } 
  19.  
  20.  public function next() 
  21.  { 
  22.  next($this->_data); 
  23.  } 
  24.  
  25.  public function rewind() 
  26.  { 
  27.  reset($this->_data); 
  28.  } 
  29.  
  30.  public function valid() 
  31.  { 
  32.  return $this->key() !== NULL; 
  33.  } 
  34. } 
  35.  
  36. $t = [ 
  37.  'yrt' => '燕睿涛', 
  38.  'name' => '燕睿涛', 
  39.  false, 
  40.  '燕睿涛' 
  41. ]; 
  42. $obj = new myObj($t); 
  43.  
  44. foreach ($obj as $key => $value) { 
  45.  echo "{$key} => ".var_export($value, true)."\n"; 
  46. } 
  47. //输出: 
  48. yrt => '燕睿涛' 
  49. name => '燕睿涛' 
  50. 0 => false 
  51. 1 => '燕睿涛' 

上面这个参考了鸟哥的一篇文章关于一笔试题(Iterator模式),不过鸟哥的那个判断valid有点瑕疵,当碰到值北来就是false的时候就会截断

总结:说了这么多好像还是没有体会到他们的用处,建议看看Yii2的源码,源码里面大量使用了这些东西,看了之后,你会慢慢觉得“哦~好像还真是挺有用的。。。。”

Tags: PHP预定义接口

分享到: