1. 迭代都知道,其实就是foreach 对一个数组,对象,进行遍历,<br />提供一种方法顺序访问一个聚合函数对象中各个元素,而又不需暴露该对象的内部表示<br />定义游标指针,操作依次指向下一个,展示当前指针数据,遍历出所有数据<br />通过while 判断,foreach 的工作
    1. interface MsgIterator
    2. {
    3. public function First();
    4. public function Next();
    5. public function IsDone();
    6. public function CurrentItem();
    7. }
    8. // 迭代器
    9. class MsgIteratorAsc implements MsgIterator
    10. {
    11. private $list;
    12. private $index;
    13. public function __construct($list)
    14. {
    15. $this->list = $list;
    16. $this->index = 0;
    17. }
    18. public function First()
    19. {
    20. $this->index = 0;
    21. }
    22. public function Next()
    23. {
    24. $this->index++;
    25. }
    26. public function IsDone()
    27. {
    28. return $this->index >= count($this->list);
    29. }
    30. public function CurrentItem()
    31. {
    32. return $this->list[$this->index];
    33. }
    34. }
    35. interface Message
    36. {
    37. public function CreateIterator($list);
    38. }
    39. class MessageAsc implements Message
    40. {
    41. public function CreateIterator($list)
    42. {
    43. return new MsgIteratorAsc($list);
    44. }
    45. }
    46. // 要发的短信号码列表
    47. $mobileList = [
    48. '13111111111',
    49. '13111111112',
    50. '13111111113',
    51. '13111111114',
    52. '13111111115',
    53. '13111111116',
    54. '13111111117',
    55. '13111111118',
    56. ];
    57. // 服务器脚本
    58. $serverA = new MessageAsc();
    59. $iteratorA = $serverA->CreateIterator($mobileList);
    60. while (!$iteratorA->IsDone()) {
    61. echo $iteratorA->CurrentItem(), PHP_EOL;
    62. $iteratorA->Next();
    63. }