12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace Core;
- /**
- * 迭代器模式
- * Class AllUser
- * @package Core
- */
- class AllUser implements \Iterator
- {
- /**
- * @var 迭代器的当前位置
- */
- protected $index;
- /**
- * @var array|mixed
- */
- protected $ids = array();
- /**
- * @var Databases\Mysqli
- */
- protected $user;
- public function __construct()
- {
- $this->user = new \Core\User();
- $this->ids = $this->user->getIds();
- }
- /**
- * 第三步:拿到当前数据
- * @return mixed|void
- */
- public function current()
- {
- // TODO: Implement current() method.
- $id = $this->ids[$this->index]['id'];
- return $this->user->getUserById($id);
- }
- /**
- * 迭代器索引增幅
- */
- public function next()
- {
- // TODO: Implement next() method.
- $this->index ++;
- }
- /**
- * @return bool|迭代器的当前位置|float|int|string|null
- */
- public function key()
- {
- // TODO: Implement key() method.
- return $this->index;
- }
- /**
- * 第一步:将索引标记在开头
- */
- public function rewind()
- {
- // TODO: Implement rewind() method.
- $this->index = 0;
- }
- /**
- * 第二步:验证是否还有下一个元素
- * @return bool|void
- */
- public function valid()
- {
- // TODO: Implement valid() method.
- return $this->index < count($this->ids);
- }
- }
|