Last active
February 16, 2016 11:09
-
-
Save useless-stuff/093d7cd7ad81f3c86920 to your computer and use it in GitHub Desktop.
PHP - LimitIterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Class RandomObject | |
| */ | |
| class RandomObject | |
| { | |
| protected $value; | |
| /** | |
| * RandomObject constructor | |
| */ | |
| public function __construct() | |
| { | |
| $this->value = rand(0, 1000); | |
| } | |
| /** | |
| * @return int | |
| */ | |
| public function getValue() | |
| { | |
| return $this->value; | |
| } | |
| /** | |
| * @param int $value | |
| */ | |
| public function setValue($value) | |
| { | |
| $this->value = $value; | |
| } | |
| } | |
| // Generate random data | |
| $objectsCollection = new ArrayIterator(); | |
| for ($i = 1; $i < 100; $i++) { | |
| $objectsCollection->append(new RandomObject()); | |
| } | |
| $limitIterator = new LimitIterator($objectsCollection, 20, 5); | |
| foreach ($limitIterator as $iterator) { | |
| print_r($iterator); | |
| } | |
| // Output | |
| /* | |
| [ | |
| { | |
| value:protected: "450" | |
| }, | |
| { | |
| value:protected: "639" | |
| }, | |
| { | |
| value:protected: "105" | |
| }, | |
| { | |
| value:protected: "852" | |
| }, | |
| { | |
| value:protected: "391" | |
| } | |
| ] | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment