AllUser.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Core;
  3. /**
  4. * 迭代器模式
  5. * Class AllUser
  6. * @package Core
  7. */
  8. class AllUser implements \Iterator
  9. {
  10. /**
  11. * @var 迭代器的当前位置
  12. */
  13. protected $index;
  14. /**
  15. * @var array|mixed
  16. */
  17. protected $ids = array();
  18. /**
  19. * @var Databases\Mysqli
  20. */
  21. protected $user;
  22. public function __construct()
  23. {
  24. $this->user = new \Core\User();
  25. $this->ids = $this->user->getIds();
  26. }
  27. /**
  28. * 第三步:拿到当前数据
  29. * @return mixed|void
  30. */
  31. public function current()
  32. {
  33. // TODO: Implement current() method.
  34. $id = $this->ids[$this->index]['id'];
  35. return $this->user->getUserById($id);
  36. }
  37. /**
  38. * 迭代器索引增幅
  39. */
  40. public function next()
  41. {
  42. // TODO: Implement next() method.
  43. $this->index ++;
  44. }
  45. /**
  46. * @return bool|迭代器的当前位置|float|int|string|null
  47. */
  48. public function key()
  49. {
  50. // TODO: Implement key() method.
  51. return $this->index;
  52. }
  53. /**
  54. * 第一步:将索引标记在开头
  55. */
  56. public function rewind()
  57. {
  58. // TODO: Implement rewind() method.
  59. $this->index = 0;
  60. }
  61. /**
  62. * 第二步:验证是否还有下一个元素
  63. * @return bool|void
  64. */
  65. public function valid()
  66. {
  67. // TODO: Implement valid() method.
  68. return $this->index < count($this->ids);
  69. }
  70. }