SlideShare a Scribd company logo
1 of 142
Looping the
Loop
with
SPL Iterators
Looping the Loop with SPL
Iterators
What is an Iterator?
$dataSet = ['A', 'B', 'C'];
foreach($dataSet as $key => $value) {
echo "{$key} => {$value}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
What is an Iterator?
0 => A
1 => B
2 => C
Looping the Loop with SPL
Iterators
What is an Iterator?
An Iterator is an object that enables a programmer to
traverse a container, particularly lists.
A Behavioural Design Pattern (GoF)
Looping the Loop with SPL
Iterators
What is an Iterator?
$data = ['A', 'B', 'C'];
$dataSet = new ArrayIterator($data);
foreach($dataSet as $key => $value) {
echo "{$key} => {$value}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
What is an Iterator?
0 => A
1 => B
2 => C
Looping the Loop with SPL
Iterators
What is an Iterator?
interface Iterator extends Traversable {
/* Methods */
abstract public current ( void ) : mixed
abstract public key ( void ) : scalar
abstract public next ( void ) : void
abstract public rewind ( void ) : void
abstract public valid ( void ) : bool
}
Looping the Loop with SPL
Iterators
What is an Iterator?
$data = ['A', 'B', 'C'];
$iterator = new ArrayIterator($data);
$iterator->rewind();
while ($iterator->valid()) {
$key = $iterator->key();
$value = $iterator->current();
echo "{$key} => {$value}", PHP_EOL;
$iterator->next();
}
Looping the Loop with SPL
Iterators
What is an Iterator?
0 => A
1 => B
2 => C
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
• Object Oriented, Iterators are Classes.
• Iterators can have meaningful class
names.
• Improves Readability of Code with type-
hinting.
• We can provide additional domain or
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
• Array are general purpose, with no
enforceable structure.
• Iterators can enforce structure and
design.
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
• Arrays use a LOT of memory.
• Iterators CAN be more memory efficient.
• Lazy Loading
• Early Break.
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
function processFromFile($fileName) {
$csvData = readCsvFile($fileName);
foreach($csvData as $order) {
processOrder($order);
}
}
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
function readCsvFile($fileName) {
$data = [];
foreach(file($fileName) as $line) {
$lineArray = str_getcsv($line);
if (count($lineArray) > 0) {
array_push($data, $lineArray);
}
}
return $data;
}
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
function readCsvFile($fileName) {
foreach (file($fileName) as $line) {
$lineArray = str_getcsv($line);
if (!empty($lineArray)) {
yield $lineArray;
}
}
}
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
function readCsvFile($fileName) {
$handle = fopen($fileName, 'rb');
do {
$lineArray = str_getcsv(fgets($handle));
if (!empty($lineArray)) {
yield $lineArray;
}
} while (!feof($handle));
fclose($handle);
}
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
PHP 5.5.33 (cli) (built: Mar 2 2016 15:20:49)
$ php testLoader1.php
Call time to load file was 0.0670 seconds
Peak memory usage: 14336 KB
$ php testLoader2.php
Call time to load file was 0.0417 seconds
Peak memory usage: 3840 KB
$ php testLoader3.php
Call time to load file was 0.0487 seconds
Peak memory usage: 256 KB
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
PHP 7.4.14 (cli) (built: Jan 5 2021 15:12:29) ( ZTS Visual C++ 2017 x64 )
$ php testLoader1.php
Call time to load file was 0.0623 seconds
Peak memory usage: 12288 KB
$ php testLoader2.php
Call time to load file was 0.0577 seconds
Peak memory usage: 4096 KB
$ php testLoader3.php
Call time to load file was 0.0601 seconds
Peak memory usage: 2048 KB
Looping the Loop with SPL
Iterators
Why use an Iterator instead of an
Array?
PHP 8.0.1 (cli) (built: Jan 5 2021 23:43:39) ( ZTS Visual C++ 2019 x64 )
$ php testLoader1.php
Call time to load file was 0.0208 seconds
Peak memory usage: 12288 KB
$ php testLoader2.php
Call time to load file was 0.0146 seconds
Peak memory usage: 4096 KB
$ php testLoader3.php
Call time to load file was 0.0163 seconds
Peak memory usage: 2048 KB
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
Looping the Loop with SPL
Iterators
The Iterable Tree
Not all iterables are equal
You can count the elements in an array !
Can you count the elements in an Iterator?
Looping the Loop with SPL
Iterators
The Iterable Tree
Not all iterables are equal
You can access the elements of an array by
their index !
Can you access the elements of an Iterator
by index ?
Looping the Loop with SPL
Iterators
The Iterable Tree
Not all iterables are equal
You can rewind an Iterator !
Can you rewind a Generator ?
Looping the Loop with SPL
Iterators
The Iterable Tree
Not all iterables are equal
Iterator_* functions (e.g. iterator_to_array)
work on Iterators ?
Can you use iterator_* functions on an
IteratorAggregate ?
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
Iterable is a pseudo-type
introduced in PHP 7.1. It accepts
any array, or any object that
implements the Traversable
interface. Both of these types are
iterable using foreach.
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
An array in PHP is an ordered map, a
type that associates values to keys. This
type is optimized for several different
uses; it can be treated as an array, list
(vector), hash table (an implementation
of a map), dictionary, collection, stack,
queue, and more.
The foreach control structure exists
specifically for arrays.
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
Abstract base interface to detect if a
class is traversable using foreach. It
cannot be implemented alone in
Userland code; but instead must be
implemented through either
IteratorAggregate or Iterator.
Internal (built-in) classes that implement
this interface can be used in a foreach
construct and do not need to implement
IteratorAggregate or Iterator.
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
Abstract base interface to detect if a
class is traversable using foreach. It
cannot be implemented alone in
Userland code; but instead must be
implemented through either
IteratorAggregate or Iterator.
PDOStatement
DatePeriod
SimpleXMLElement
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
An Interface for external iterators or
objects that can be iterated themselves
internally.
PHP already provides a number of
iterators for many day to day tasks
within the Standard PHP Library (SPL).
SPL Iterators
SPL DataStructures
WeakMaps (PHP 8)
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
Generators provide an easy way to
implement simple user-defined iterators
without the overhead or complexity of
implementing a class that implements
the Iterator interface.
This flexibility does come at a cost,
however: generators are forward-only
iterators, and cannot be rewound once
iteration has started.
Looping the Loop with SPL
Iterators
The Iterable Tree
iterable
array
Traversable
Iterator
Generator
IteratorAggregate
IteratorAggregate is a special interface
for implementing an abstraction layer
between user code and iterators.
Implementing an IteratorAggregate will
allow you to delegate the work of
iteration to a separate class, while still
enabling you to use the collection inside
a foreach loop.
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
Implementing an Iterator
interface Iterator extends Traversable {
/* Methods */
abstract public current ( void ) : mixed
abstract public key ( void ) : scalar
abstract public next ( void ) : void
abstract public rewind ( void ) : void
abstract public valid ( void ) : bool
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
class Utf8StringIterator implements Iterator {
private $string;
private $offset = 0;
private $length = 0;
public function __construct(string $string) {
$this->string = $string;
$this->length = iconv_strlen($string);
}
public function valid(): bool {
return $this->offset < $this->length;
}
...
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
class Utf8StringIterator implements Iterator {
...
public function rewind(): void {
$this->offset = 0;
}
public function current(): string {
return iconv_substr($this->string, $this->offset, 1);
}
public function key(): int {
return $this->offset;
}
public function next(): void {
++$this->offset;
}
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
$stringIterator = new Utf8StringIterator('Καλησπέρα σε όλους');
foreach($stringIterator as $character) {
echo "[{$character}]";
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
[Κ][α][λ][η][σ][π][έ][ρ][α][ ][σ][ε][ ][ό][λ][ο][υ][ς]
Looping the Loop with SPL
Iterators
Implementing an Iterator
class FileLineIterator implements Iterator {
private $fileHandle;
private $line = 1;
public function __construct(string $fileName) {
$this->fileHandle = fopen($fileName, 'r');
}
public function __destruct() {
fclose($this->fileHandle);
}
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
class FileLineIterator implements Iterator {
...
public function current(): string {
$fileData = fgets($this->fileHandle);
return rtrim($fileData, "n");
}
public function key(): int {
return $this->line;
}
public function next(): void {
++$this->line;
}
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
class FileLineIterator implements Iterator {
...
public function rewind(): void {
fseek($this->fileHandle, 0);
$this->line = 1;
}
public function valid(): bool {
return !feof($this->fileHandle);
}
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
$fileLineIterator = new FileLineIterator(__FILE__);
foreach($fileLineIterator as $lineNumber => $fileLine) {
echo "{$lineNumber}: {$fileLine}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
Implementing an Iterator
1: <?php
2:
3: class FileLineIterator implements Iterator {
4: private $fileHandle;
5: private $line = 1;
6:
7: public function __construct(string $fileName) {
8: $this->fileHandle = fopen($fileName, 'r’);
9: }
10:
…
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
public function all(): array {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
$statement->execute();
$users = $statement->fetchAll();
return array_map(fn(array $user): User => User::fromArray($user), $users);
}
}
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
/**
* @return User[]
*/
public function all(): array {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
$statement->execute();
$users = $statement->fetchAll();
return array_map(fn(array $user): User => User::fromArray($user), $users);
}
}
Looping the Loop with SPL
Iterators
class UserCollection extends ArrayIterator {
}
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
public function all(): UserCollection {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
$statement->execute();
$users = $statement->fetchAll();
return new UserCollection(
array_map(fn(array $user): User => User::fromArray($user), $users)
);
}
}
Looping the Loop with SPL
Iterators
class UserCollection extends ArrayIterator {
public function current(): User {
return User::fromArray(parent::current());
}
}
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
public function all(): UserCollection {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
$statement->execute();
$users = $statement->fetchAll();
return new UserCollection($users);
}
}
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
ArrayIterator
Implements:
Countable
ArrayAccess
Serializable
SeekableIterator (which extends Iterator)
Looping the Loop with SPL
Iterators
ArrayIterator
Implements:
Countable
Looping the Loop with SPL
Iterators
ArrayIterator – Countable
interface Countable {
/* Methods */
abstract public count ( void ) : int
}
Looping the Loop with SPL
Iterators
ArrayIterator – Countable
$userData = ['user1', 'user2', 'user3', 'user4'];
$userList = new UserList($userData);
$userCount = count($userList);
echo "There are {$userCount} users in this list", PHP_EOL;
Looping the Loop with SPL
Iterators
ArrayIterator – Countable
$userData = ['user1', 'user2', 'user3', 'user4'];
$userList = new UserList($userData);
echo "There are {$userList->count()} users in this list", PHP_EOL;
Looping the Loop with SPL
Iterators
ArrayIterator – Countable
There are 4 users in this list
Looping the Loop with SPL
Iterators
ArrayIterator
Implements:
Countable
ArrayAccess
Looping the Loop with SPL
Iterators
ArrayIterator – ArrayAccess
interface ArrayAccess {
/* Methods */
abstract public offsetExists ( mixed $offset ) : bool
abstract public offsetGet ( mixed $offset ) : mixed
abstract public offsetSet ( mixed $offset , mixed $value ) : void
abstract public offsetUnset ( mixed $offset ) : void
}
Looping the Loop with SPL
Iterators
ArrayIterator – ArrayAccess
$userData = ['user1', 'user2', 'user3', 'user4'];
$userList = new UserList($userData);
echo "The 3rd entry in the list is {$userList[2]}", PHP_EOL;
Looping the Loop with SPL
Iterators
ArrayIterator – ArrayAccess
The 3rd entry in the list is user3
Looping the Loop with SPL
Iterators
ArrayIterator
Implements:
Countable
ArrayAccess
Serializable
Looping the Loop with SPL
Iterators
ArrayIterator – Serializable
interface Serializable {
/* Methods */
abstract public serialize ( void ) : string
abstract public unserialize ( string $serialized ) : void
}
Looping the Loop with SPL
Iterators
ArrayIterator – Serializable
$userData = ['user1', 'user2', 'user3', 'user4'];
$userList = new UserList($userData);
var_dump(serialize($userList));
Looping the Loop with SPL
Iterators
ArrayIterator – Serializable
string(117)
"O:8:"UserList":4:{i:0;i:0;i:1;a:4:{i:0;s:5:"user1";
i:1;s:5:"user2";i:2;s:5:"user3";i:3;s:5:"user4";}i:2;
a:0:{}i:3;N;}"
Looping the Loop with SPL
Iterators
ArrayIterator
Implements:
Countable
ArrayAccess
Serializable
SeekableIterator (which extends Iterator)
Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
SeekableIterator extends Iterator {
/* Methods */
abstract public seek ( int $position ) : void
/* Inherited methods */
abstract public Iterator::current ( void ) : mixed
abstract public Iterator::key ( void ) : scalar
abstract public Iterator::next ( void ) : void
abstract public Iterator::rewind ( void ) : void
abstract public Iterator::valid ( void ) : bool
}
Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
$userData = ['user1', 'user2', 'user3', 'user4'];
$userList = new UserList($userData);
$userList->seek(2);
while ($userList->valid()) {
echo $userList->current(), PHP_EOL;
$userList->next();
}
Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
user3
user4
Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
$userData = ['user1', 'user2', 'user3', 'user4'];
$userList = new UserList($userData);
$userList->seek(2);
foreach($userList as $userName) {
echo $userName, PHP_EOL;
}
Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
user1
user2
user3
user4

Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
$userData = ['A' => 'user1', 'B' => 'user2', 'C' => 'user3', 'D' => 'user4'];
$userList = new UserList($userData);
$userList->seek('C');
while ($userList->valid()) {
echo $userList->current(), PHP_EOL;
$userList->next();
}
Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
PHP7
Warning:
ArrayIterator::seek() expects parameter 1 to be int,
string given

Looping the Loop with SPL
Iterators
ArrayIterator – SeekableIterator
PHP8
Fatal error: Uncaught TypeError
ArrayIterator::seek(): Argument #1 ($position) must be
of type int, string given

Looping the Loop with SPL
Iterators
ArrayIterator
Implements:
Countable
ArrayAccess
Serializable
SeekableIterator (which extends Iterator)
Also provides methods for sorting
Doesn’t work with array_* functions

Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
LimitIterator
$userData = ['user1', 'user2', 'user3', 'user4'];
$userList = new LimitIterator(
new UserList($userData),
2,
1
);
foreach($userList as $value) {
echo $value, PHP_EOL;
}
Looping the Loop with SPL
Iterators
LimitIterator
user3
Looping the Loop with SPL
Iterators
InfiniteIterator
$weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
$todayOffset = (int) (new DateTime('2020-09-08'))->format('w');
$weekdayList = new LimitIterator(
new InfiniteIterator( new ArrayIterator($weekdayNames) ),
$todayOffset,
14
);
foreach($weekdayList as $dayOfWeek) {
echo $dayOfWeek, PHP_EOL;
}
Looping the Loop with SPL
Iterators
InfiniteIterator
Tues
Wed
Thurs
Fri
Sat
Sun
Mon
Tues
Wed
Thurs
Fri
Sat
Sun
Mon
Looping the Loop with SPL
Iterators
CallbackFilterIterator
$weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
$todayOffset = (int) (new DateTime('2020-09-15'))->format('w');
$workdayList = new CallbackFilterIterator(
new LimitIterator(
new InfiniteIterator( new ArrayIterator($weekdayNames) ),
$todayOffset,
14
),
fn($dayName): bool => $dayName !== 'Sat' && $dayName !== 'Sun'
);
foreach($workdayList as $dayOfWeek) {
echo $dayOfWeek, PHP_EOL;
}
Looping the Loop with SPL
Iterators
CallbackFilterIterator
Tues
Wed
Thurs
Fri
Mon
Tues
Wed
Thurs
Fri
Mon
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
FilterIterator
abstract FilterIterator extends IteratorIterator implements OuterIterator {
/* Methods */
public abstract accept ( void ) : bool
public __construct ( Iterator $iterator )
public current ( void ) : mixed
public getInnerIterator ( void ) : Iterator
public key ( void ) : mixed
public next ( void ) : void
public rewind ( void ) : void
public valid ( void ) : bool
}
Looping the Loop with SPL
Iterators
FilterIterator
class ImageFileIterator extends FilterIterator {
// Overwrite the constructor to automagically create an inner iterator
// to iterate the file system when given a path name.
public function __construct(string $path) {
parent::__construct(new FilesystemIterator($path));
}
// Overwrite the accept method to perform a super simple test to
// determine if the files found were images or not.
public function accept(): bool {
$fileObject = $this->getInnerIterator()->current();
return preg_match('/^(?:gif|jpe?g|png)$/i', $fileObject->getExtension());
}
}
Looping the Loop with SPL
Iterators
FilterIterator
class ImageFileIterator extends FilterIterator {
// Overwrite the constructor to automagically create an inner iterator
// to iterate the file system when given a path name.
public function __construct(string $path) {
parent::__construct(new FilesystemIterator($path));
}
// Overwrite the accept method to perform a super simple test to
// determine if the files found were images or not.
public function accept(): bool {
return preg_match('/^(?:gif|jpe?g|png)$/i', $this->getExtension());
}
}
Looping the Loop with SPL
Iterators
FilterIterator
$path = dirname(__FILE__);
foreach(new ImageFileIterator($path) as $imageObject) {
echo $imageObject->getBaseName(), PHP_EOL;
}
Looping the Loop with SPL
Iterators
FilterIterator
anubis.jpg
Carpet.jpg
Crazy Newspaper Headlines.jpg
elephpant.jpg
Pie Chart.jpg
PowerOfBooks.jpg
Struggling-Team.png
TeddyBears.jpg
Looping the Loop with SPL
Iterators
Iterator Keys
class ImageFileIterator extends FilterIterator {
...
// Overwrite the key method to return the file extension as the key
public function key(): string {
return $this->getExtension();
}
}
foreach(new ImageFileIterator($path) as $extension => $imageObject) {
echo "{$extension} => {$imageObject->getBaseName()}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
Iterator Keys
jpg => anubis.jpg
jpg => Carpet.jpg
jpg => Crazy Newspaper Headlines.jpg
jpg => elephpant.jpg
jpg => Pie Chart.jpg
jpg => PowerOfBooks.jpg
png => Struggling-Team.png
jpg => TeddyBears.jpg
Looping the Loop with SPL
Iterators
Iterator Keys
class ImageFileIterator extends FilterIterator {
...
// Overwrite the key method to return the last modified date/time as the key
public function key(): DateTime {
return DateTime::createFromFormat('U', $this->getMTime());
}
}
foreach(new ImageFileIterator($path) as $date => $imageObject) {
echo "{$date->format('Y-m-d H:i:s')} => {$imageObject->getBaseName()}",
PHP_EOL;
}
Looping the Loop with SPL
Iterators
Iterator Keys
2012-03-24 00:38:24 => anubis.jpg
2013-01-27 22:32:20 => Carpet.jpg
2013-03-07 12:16:24 => Crazy Newspaper Headlines.jpg
2012-02-16 22:31:56 => elephpant.jpg
2012-10-09 11:51:56 => Pie Chart.jpg
2012-07-17 12:22:32 => PowerOfBooks.jpg
2012-12-30 18:14:24 => Struggling-Team.png
2012-01-31 11:17:32 => TeddyBears.jpg
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
MultipleIterator
$dates = ['Q1-2019', 'Q2-2019', 'Q3-2019', 'Q4-2019'];
$budget = [1200, 1300, 1400, 1500];
$expenditure = [1250, 1315, 1440, 1485];
$budgetElements = count($budget);
for ($i = 0; $i < $budgetElements; $i++) {
echo "{$dates[$i]} - {$budget[$i]}, {$expenditure[$i]}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
MultipleIterator
$dates = ['Q1-2019', 'Q2-2019', 'Q3-2019', 'Q4-2019'];
$budget = [1200, 1300, 1400, 1500];
$expenditure = [1250, 1315, 1440, 1485];
foreach($dates as $key => $period) {
echo "{$period} - {$budget[$key]}, {$expenditure[$key]}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
MultipleIterator
Q1-2019 - 1200, 1250
Q2-2019 - 1300, 1315
Q3-2019 - 1400, 1440
Q4-2019 - 1500, 1485
Looping the Loop with SPL
Iterators
MultipleIterator
function wrapArrayAsIterator(array $array) {
return new ArrayIterator($array);
}
$combinedIterable = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$combinedIterable->attachIterator(wrapArrayAsIterator($dates), 'date');
$combinedIterable->attachIterator(wrapArrayAsIterator($budget), 'budget');
$combinedIterable->attachIterator(wrapArrayAsIterator($expenditure), 'expenditure');
Looping the Loop with SPL
Iterators
MultipleIterator
foreach($combinedIterable as $dateValues) {
echo $dateValues['date'],
" - {$dateValues['budget']}, {$dateValues['expenditure']}",
PHP_EOL;
}
Looping the Loop with SPL
Iterators
MultipleIterator
foreach($combinedIterable as $dateValues) {
[$date, $budget, $expenditure] = $dateValues;
echo "{$date} - {$budget}, {$expenditure}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
MultipleIterator
Q1-2019 - 1200, 1250
Q2-2019 - 1300, 1315
Q3-2019 - 1400, 1440
Q4-2019 - 1500, 1485
Looping the Loop with SPL
Iterators
MultipleIterator
function wrapArrayAsIterator(array $array) {
return new ArrayIterator($array);
}
$combinedIterable = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$combinedIterable->attachIterator(wrapArrayAsIterator($dates), 'date');
$combinedIterable->attachIterator(wrapArrayAsIterator($budget), 'budget');
$combinedIterable->attachIterator(wrapArrayAsIterator($expenditure), 'expenditure');
Looping the Loop with SPL
Iterators
MultipleIterator
foreach($combinedIterable as $dateValues) {
extract($dateValues);
echo "{$date} - {$budget}, {$expenditure}", PHP_EOL;
}
Looping the Loop with SPL
Iterators
MultipleIterator
Q1-2019 - 1200, 1250
Q2-2019 - 1300, 1315
Q3-2019 - 1400, 1440
Q4-2019 - 1500, 1485
Looping the Loop with SPL
Iterators
MultipleIterator
$allocations = $totalAdvance->allocateTo(count($cars));
foreach ($cars as $i => $car) {
$car->setAdvance($allocations[$i]);
}
Looping the Loop with SPL
Iterators
MultipleIterator
$allocations = $totalAdvance->allocateTo(count($cars));
$advanceAllocations = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$advanceAllocations->attachIterator(ArrayIterator($cars), 'car');
$advanceAllocations->attachIterator(ArrayIterator($allocations), 'advance');
foreach($advanceAllocations as $advanceAllocation) {
extract($advanceAllocation);
$car->setAdvance($advance);
}
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
Inner and Outer Iterators
Inner Iterator
Implements Iterator, or extends a class that
implements Iterator
ArrayIterator
SeekableIterator
FilesystemIterator
MultipleIterator
Looping the Loop with SPL
Iterators
Inner and Outer Iterators
Outer Iterator
Implements OuterIterator, or extends an
Iterator that already implements
OuterIterator
LimitIterator
InfiniteIterator
FilterIterator
CallbackFilterIterator
Looping the Loop with SPL
Iterators
Recursive Iterators
A RecursiveIterator defines methods to
allow iteration over hierarchical data
structures.
Nested Arrays (and Objects if using
ArrayAccess)
Filesystem Directories/Folders
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
public function all(): UserCollection {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
$statement->execute();
$users = $statement->fetchAll();
return new UserCollection(
array_map(fn(array $user): User => User::fromArray($user), $users)
);
}
}
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
public function all(): Generator {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
$statement->execute();
while ($user = $statement->fetch()) {
yield User::fromArray($user);
}
}
}
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
/**
* @return Generator|User[]
*/
public function all(): Generator {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
$statement->execute();
while ($user = $statement->fetch()) {
yield User::fromArray($user);
}
}
}
Looping the Loop with SPL
Iterators
IteratorAggregate
IteratorAggregate extends Traversable {
/* Methods */
abstract public getIterator ( void ) : Traversable
}
Looping the Loop with SPL
Iterators
class UserCollection implements IteratorAggregate {
private PDOStatement $pdoStatement;
public function __construct(PDOStatement $pdoStatement) {
$this->pdoStatement = $pdoStatement;
}
public function getIterator(): Traversable {
$this->pdoStatement->execute();
while ($user = $this->pdoStatement->fetch()) {
yield User::fromArray($user);
}
}
}
Looping the Loop with SPL
Iterators
class UserRepository {
// ...
public function all(): UserCollection {
$userQuery = 'SELECT * FROM `users` ...';
$statement = $this->pdo->prepare($userQuery);
return new UserCollection($statement);
}
}
Looping the Loop with SPL
Iterators
Implementing a Fluent Nested Iterator
$weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
$todayOffset = (int) (new DateTime('2020-09-15'))->format('w');
$workdayList = new CallbackFilterIterator(
new LimitIterator(
new InfiniteIterator( new ArrayIterator($weekdayNames) ),
$todayOffset,
14
),
fn($dayName): bool => $dayName !== 'Sat' && $dayName !== 'Sun'
);
foreach($workdayList as $dayOfWeek) {
echo $dayOfWeek, PHP_EOL;
}
Looping the Loop with SPL
Iterators
Implementing a Fluent Nested Iterator
class FluentNestedIterator implements IteratorAggregate {
protected iterable $iterator;
public function __construct(iterable $iterator) {
$this->iterator = $iterator;
}
public function with(string $iterable, ...$args): self {
$this->iterator = new $iterable($this->iterator, ...$args);
return $this;
}
public function getIterator() {
return $this->iterator;
}
}
Looping the Loop with SPL
Iterators
Implementing a Fluent Nested Iterator
$weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
$todayOffset = (int) (new DateTime('2020-10-08'))->format('w');
$workdayList = (new FluentNestedIterator(new ArrayIterator($weekdayNames)))
->with(InfiniteIterator::class)
->with(LimitIterator::class, $todayOffset, 14)
->with(
CallbackFilterIterator::class,
fn($dayName): bool => $dayName !== 'Sat' && $dayName !== 'Sun'
);
foreach($workdayList as $dayOfWeek) {
echo $dayOfWeek, PHP_EOL;
}
Looping the Loop with SPL
Iterators
Implementing a Fluent Nested Iterator
Thurs
Fri
Mon
Tues
Wed
Thurs
Fri
Mon
Tues
Wed
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
Array Functions
Don’t work with Traversables

Looping the Loop with SPL
Iterators
Array Functions
Don’t work with Traversables

Looping the Loop with SPL
Iterators
Array Functions – Filter
function filter(iterable $iterator, Callable $callback = null) {
if ($callback === null) {
$callback = 'iterableFilterNotEmpty';
}
foreach ($iterator as $key => $value) {
if ($callback($value)) {
yield $key => $value;
}
}
}
Looping the Loop with SPL
Iterators
Array Functions – Filter
$startTime = new DateTime('2015-11-23 13:20:00Z');
$endTime = new DateTime('2015-11-23 13:30:00Z');
$timeFilter = function($timestamp) use ($startTime, $endTime) {
return $timestamp >= $startTime && $timestamp <= $endTime;
};
$filteredTrackingData = filter(
$gpxReader->getElements('trkpt'),
$timeFilter,
ARRAY_FILTER_USE_KEY
);
foreach ($filteredTrackingData as $time => $element) {
...
}
Looping the Loop with SPL
Iterators
Array Functions – Filter
2015-11-23 13:24:40
latitude: 53.5441 longitude: -2.7416 elevation: 124
2015-11-23 13:24:49
latitude: 53.5441 longitude: -2.7416 elevation: 122
2015-11-23 13:24:59
latitude: 53.5441 longitude: -2.7416 elevation: 120
2015-11-23 13:25:09
latitude: 53.5441 longitude: -2.7417 elevation: 120
2015-11-23 13:25:19
latitude: 53.5441 longitude: -2.7417 elevation: 121
2015-11-23 13:25:29
latitude: 53.5441 longitude: -2.7417 elevation: 120
2015-11-23 13:25:39
latitude: 53.5441 longitude: -2.7417 elevation: 120
Looping the Loop with SPL
Iterators
Array Functions – Filter
Of course, we could always use a
CallBackFilterIterator
Looping the Loop with SPL
Iterators
Array Functions – Map
function map(Callable $callback, iterable $iterator) {
foreach ($iterator as $key => $value) {
yield $key => $callback($value);
}
}
Looping the Loop with SPL
Iterators
Array Functions – Map
$distanceCalculator = new GpxReaderHelpersDistanceCalculator();
$mappedTrackingData = map(
[$distanceCalculator, 'setDistanceProperty'],
$gpxReader->getElements('trkpt')
);
foreach ($mappedTrackingData as $time => $element) {
...
}
Looping the Loop with SPL
Iterators
Array Functions – Map
2015-11-23 14:57:07
latitude: 53.5437 longitude: -2.7424 elevation: 103
distance from previous point: 6.93 m
2015-11-23 14:57:17
latitude: 53.5437 longitude: -2.7424 elevation: 100
distance from previous point: 1.78 m
2015-11-23 14:57:27
latitude: 53.5438 longitude: -2.7425 elevation: 89
distance from previous point: 11.21 m
2015-11-23 14:57:37
latitude: 53.5439 longitude: -2.7424 elevation: 83
distance from previous point: 9.23 m
2015-11-23 14:57:47
latitude: 53.5439 longitude: -2.7425 elevation: 92
distance from previous point: 5.40 m
Looping the Loop with SPL
Iterators
Array Functions – Walk
iterator_apply()
iterator_apply(
Traversable $iterator,
Callable $callback
[, array $args = NULL ]
) : int
Looping the Loop with SPL
Iterators
Array Functions – Reduce
function reduce(iterable $iterator, Callable $callback, $initial = null) {
$result = $initial;
foreach($iterator as $value) {
$result = $callback($result, $value);
}
return $result;
}
Looping the Loop with SPL
Iterators
Array Functions – Reduce
$distanceCalculator = new GpxReaderHelpersDistanceCalculator();
$totalDistance = reduce(
map(
[$distanceCalculator, 'setDistanceProperty'],
$gpxReader->getElements('trkpt')
),
function($runningTotal, $value) {
return $runningTotal + $value->distance;
},
0.0
);
Looping the Loop with SPL
Iterators
Array Functions – Reduce
Total distance travelled is 4.33 km
Looping the Loop with SPL
Iterators
Array Functions – Other Iterator
Functions
Iterator_count()
iterator_count ( Traversable $iterator ) : int
Looping the Loop with SPL
Iterators
Array Functions – Other Iterator
Functions
Iterator_to_array()
iterator_to_array (
Traversable $iterator
[, bool $use_keys = TRUE ]
) : array
Looping the Loop with SPL
Iterators
Looping the Loop with SPL
Iterators
A Functional Guide to Cat Herding with PHP
Generators
• https://markbakeruk.net/2016/01/19/a-functional-guide-to-cat-herding-with-php-generators/
• https://markbakeruk.net/2020/01/05/filtering-and-mapping-with-spl-iterators/
• https://markbakeruk.net/2019/12/31/parallel-looping-in-php-with-spls-multipleiterator/
Looping the Loop with SPL
Iterators
Iterating PHP Iterators
By Cal Evans
https://leanpub.com/iteratingphpiterators
by Joshua Thijssen
https://www.phparch.com/books/mastering-the-spl-library/
Who am I?
Mark Baker
Senior Software Engineer
Recharge.com, Amsterdam
Coordinator and Developer of:
Open Source PHPOffice library
PHPSpreadsheet, PHPWord, PHPPresentation, PHPProject, PHPVisio
Minor contributor to PHP core (SPL DataStructures)
Other small open source libraries available on github
@Mark_Baker
https://github.com/MarkBaker
http://uk.linkedin.com/pub/mark-baker/b/572/171
http://markbakeruk.net
Looping the
Loop
with
SPL Iterators

More Related Content

What's hot

Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Nikita Popov
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)Nikita Popov
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Kris Wallsmith
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsMark Baker
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleHugo Hamon
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)Nikita Popov
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Kris Wallsmith
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8PrinceGuru MS
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2Hugo Hamon
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 

What's hot (20)

Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
Symfony 2.0 on PHP 5.3
Symfony 2.0 on PHP 5.3Symfony 2.0 on PHP 5.3
Symfony 2.0 on PHP 5.3
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
New in php 7
New in php 7New in php 7
New in php 7
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Php functions
Php functionsPhp functions
Php functions
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Nubilus Perl
Nubilus PerlNubilus Perl
Nubilus Perl
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 

Similar to Looping the Loop with SPL Iterators

Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsMark Baker
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Elizabeth Smith
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsMark Baker
 
Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your codeElizabeth Smith
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09Elizabeth Smith
 
Code transformation With Spoon
Code transformation With SpoonCode transformation With Spoon
Code transformation With SpoonGérard Paligot
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)mircodotta
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongPROIDEA
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法x1 ichi
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code ExamplesNaresh Chintalcheru
 
SOLID mit Java 8
SOLID mit Java 8SOLID mit Java 8
SOLID mit Java 8Roland Mast
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Leveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentLeveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentJim Mlodgenski
 

Similar to Looping the Loop with SPL Iterators (20)

Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your code
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
 
Code transformation With Spoon
Code transformation With SpoonCode transformation With Spoon
Code transformation With Spoon
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
 
Java q ref 2018
Java q ref 2018Java q ref 2018
Java q ref 2018
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
 
Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
SOLID mit Java 8
SOLID mit Java 8SOLID mit Java 8
SOLID mit Java 8
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Java 8
Java 8Java 8
Java 8
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Leveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentLeveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL Environment
 
Core java
Core javaCore java
Core java
 

More from Mark Baker

Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to ProductionMark Baker
 
A Brief History of Elephpants
A Brief History of ElephpantsA Brief History of Elephpants
A Brief History of ElephpantsMark Baker
 
Aspects of love slideshare
Aspects of love slideshareAspects of love slideshare
Aspects of love slideshareMark Baker
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Mark Baker
 
A Brief History of ElePHPants
A Brief History of ElePHPantsA Brief History of ElePHPants
A Brief History of ElePHPantsMark Baker
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding HorrorsMark Baker
 
Anonymous classes2
Anonymous classes2Anonymous classes2
Anonymous classes2Mark Baker
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the UntestableMark Baker
 
Anonymous Classes: Behind the Mask
Anonymous Classes: Behind the MaskAnonymous Classes: Behind the Mask
Anonymous Classes: Behind the MaskMark Baker
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Mark Baker
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding HorrorsMark Baker
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Mark Baker
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPantMark Baker
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsMark Baker
 
SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015Mark Baker
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsMark Baker
 
Flying under the radar
Flying under the radarFlying under the radar
Flying under the radarMark Baker
 
Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Mark Baker
 

More from Mark Baker (20)

Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
A Brief History of Elephpants
A Brief History of ElephpantsA Brief History of Elephpants
A Brief History of Elephpants
 
Aspects of love slideshare
Aspects of love slideshareAspects of love slideshare
Aspects of love slideshare
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
A Brief History of ElePHPants
A Brief History of ElePHPantsA Brief History of ElePHPants
A Brief History of ElePHPants
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
Anonymous classes2
Anonymous classes2Anonymous classes2
Anonymous classes2
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
Anonymous Classes: Behind the Mask
Anonymous Classes: Behind the MaskAnonymous Classes: Behind the Mask
Anonymous Classes: Behind the Mask
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPant
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015SPL - The Undiscovered Library - PHPBarcelona 2015
SPL - The Undiscovered Library - PHPBarcelona 2015
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 
Flying under the radar
Flying under the radarFlying under the radar
Flying under the radar
 
Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)
 

Recently uploaded

Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Andreas Granig
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletAndrea Goulet
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Gáspár Nagy
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Soroosh Khodami
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfWSO2
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024Shane Coughlan
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMarkus Moeller
 
IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024vaibhav130304
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...OnePlan Solutions
 
What need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java DevelopersWhat need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java DevelopersEmilyJiang23
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024SimonedeGijt
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)Max Lee
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesNeo4j
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...naitiksharma1124
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAShane Coughlan
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationHelp Desk Migration
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckMarc Lester
 

Recently uploaded (20)

Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 
What is an API Development- Definition, Types, Specifications, Documentation.pdf
What is an API Development- Definition, Types, Specifications, Documentation.pdfWhat is an API Development- Definition, Types, Specifications, Documentation.pdf
What is an API Development- Definition, Types, Specifications, Documentation.pdf
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
 
What need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java DevelopersWhat need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java Developers
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
 

Looping the Loop with SPL Iterators

  • 2. Looping the Loop with SPL Iterators What is an Iterator? $dataSet = ['A', 'B', 'C']; foreach($dataSet as $key => $value) { echo "{$key} => {$value}", PHP_EOL; }
  • 3. Looping the Loop with SPL Iterators What is an Iterator? 0 => A 1 => B 2 => C
  • 4. Looping the Loop with SPL Iterators What is an Iterator? An Iterator is an object that enables a programmer to traverse a container, particularly lists. A Behavioural Design Pattern (GoF)
  • 5. Looping the Loop with SPL Iterators What is an Iterator? $data = ['A', 'B', 'C']; $dataSet = new ArrayIterator($data); foreach($dataSet as $key => $value) { echo "{$key} => {$value}", PHP_EOL; }
  • 6. Looping the Loop with SPL Iterators What is an Iterator? 0 => A 1 => B 2 => C
  • 7. Looping the Loop with SPL Iterators What is an Iterator? interface Iterator extends Traversable { /* Methods */ abstract public current ( void ) : mixed abstract public key ( void ) : scalar abstract public next ( void ) : void abstract public rewind ( void ) : void abstract public valid ( void ) : bool }
  • 8. Looping the Loop with SPL Iterators What is an Iterator? $data = ['A', 'B', 'C']; $iterator = new ArrayIterator($data); $iterator->rewind(); while ($iterator->valid()) { $key = $iterator->key(); $value = $iterator->current(); echo "{$key} => {$value}", PHP_EOL; $iterator->next(); }
  • 9. Looping the Loop with SPL Iterators What is an Iterator? 0 => A 1 => B 2 => C
  • 10. Looping the Loop with SPL Iterators
  • 11. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? • Object Oriented, Iterators are Classes. • Iterators can have meaningful class names. • Improves Readability of Code with type- hinting. • We can provide additional domain or
  • 12. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? • Array are general purpose, with no enforceable structure. • Iterators can enforce structure and design.
  • 13. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? • Arrays use a LOT of memory. • Iterators CAN be more memory efficient. • Lazy Loading • Early Break.
  • 14. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? function processFromFile($fileName) { $csvData = readCsvFile($fileName); foreach($csvData as $order) { processOrder($order); } }
  • 15. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? function readCsvFile($fileName) { $data = []; foreach(file($fileName) as $line) { $lineArray = str_getcsv($line); if (count($lineArray) > 0) { array_push($data, $lineArray); } } return $data; }
  • 16. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? function readCsvFile($fileName) { foreach (file($fileName) as $line) { $lineArray = str_getcsv($line); if (!empty($lineArray)) { yield $lineArray; } } }
  • 17. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? function readCsvFile($fileName) { $handle = fopen($fileName, 'rb'); do { $lineArray = str_getcsv(fgets($handle)); if (!empty($lineArray)) { yield $lineArray; } } while (!feof($handle)); fclose($handle); }
  • 18. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? PHP 5.5.33 (cli) (built: Mar 2 2016 15:20:49) $ php testLoader1.php Call time to load file was 0.0670 seconds Peak memory usage: 14336 KB $ php testLoader2.php Call time to load file was 0.0417 seconds Peak memory usage: 3840 KB $ php testLoader3.php Call time to load file was 0.0487 seconds Peak memory usage: 256 KB
  • 19. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? PHP 7.4.14 (cli) (built: Jan 5 2021 15:12:29) ( ZTS Visual C++ 2017 x64 ) $ php testLoader1.php Call time to load file was 0.0623 seconds Peak memory usage: 12288 KB $ php testLoader2.php Call time to load file was 0.0577 seconds Peak memory usage: 4096 KB $ php testLoader3.php Call time to load file was 0.0601 seconds Peak memory usage: 2048 KB
  • 20. Looping the Loop with SPL Iterators Why use an Iterator instead of an Array? PHP 8.0.1 (cli) (built: Jan 5 2021 23:43:39) ( ZTS Visual C++ 2019 x64 ) $ php testLoader1.php Call time to load file was 0.0208 seconds Peak memory usage: 12288 KB $ php testLoader2.php Call time to load file was 0.0146 seconds Peak memory usage: 4096 KB $ php testLoader3.php Call time to load file was 0.0163 seconds Peak memory usage: 2048 KB
  • 21. Looping the Loop with SPL Iterators
  • 22. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate
  • 23. Looping the Loop with SPL Iterators The Iterable Tree Not all iterables are equal You can count the elements in an array ! Can you count the elements in an Iterator?
  • 24. Looping the Loop with SPL Iterators The Iterable Tree Not all iterables are equal You can access the elements of an array by their index ! Can you access the elements of an Iterator by index ?
  • 25. Looping the Loop with SPL Iterators The Iterable Tree Not all iterables are equal You can rewind an Iterator ! Can you rewind a Generator ?
  • 26. Looping the Loop with SPL Iterators The Iterable Tree Not all iterables are equal Iterator_* functions (e.g. iterator_to_array) work on Iterators ? Can you use iterator_* functions on an IteratorAggregate ?
  • 27. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate Iterable is a pseudo-type introduced in PHP 7.1. It accepts any array, or any object that implements the Traversable interface. Both of these types are iterable using foreach.
  • 28. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate An array in PHP is an ordered map, a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and more. The foreach control structure exists specifically for arrays.
  • 29. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate Abstract base interface to detect if a class is traversable using foreach. It cannot be implemented alone in Userland code; but instead must be implemented through either IteratorAggregate or Iterator. Internal (built-in) classes that implement this interface can be used in a foreach construct and do not need to implement IteratorAggregate or Iterator.
  • 30. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate Abstract base interface to detect if a class is traversable using foreach. It cannot be implemented alone in Userland code; but instead must be implemented through either IteratorAggregate or Iterator. PDOStatement DatePeriod SimpleXMLElement
  • 31. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate An Interface for external iterators or objects that can be iterated themselves internally. PHP already provides a number of iterators for many day to day tasks within the Standard PHP Library (SPL). SPL Iterators SPL DataStructures WeakMaps (PHP 8)
  • 32. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate Generators provide an easy way to implement simple user-defined iterators without the overhead or complexity of implementing a class that implements the Iterator interface. This flexibility does come at a cost, however: generators are forward-only iterators, and cannot be rewound once iteration has started.
  • 33. Looping the Loop with SPL Iterators The Iterable Tree iterable array Traversable Iterator Generator IteratorAggregate IteratorAggregate is a special interface for implementing an abstraction layer between user code and iterators. Implementing an IteratorAggregate will allow you to delegate the work of iteration to a separate class, while still enabling you to use the collection inside a foreach loop.
  • 34. Looping the Loop with SPL Iterators
  • 35. Looping the Loop with SPL Iterators Implementing an Iterator interface Iterator extends Traversable { /* Methods */ abstract public current ( void ) : mixed abstract public key ( void ) : scalar abstract public next ( void ) : void abstract public rewind ( void ) : void abstract public valid ( void ) : bool }
  • 36. Looping the Loop with SPL Iterators Implementing an Iterator class Utf8StringIterator implements Iterator { private $string; private $offset = 0; private $length = 0; public function __construct(string $string) { $this->string = $string; $this->length = iconv_strlen($string); } public function valid(): bool { return $this->offset < $this->length; } ... }
  • 37. Looping the Loop with SPL Iterators Implementing an Iterator class Utf8StringIterator implements Iterator { ... public function rewind(): void { $this->offset = 0; } public function current(): string { return iconv_substr($this->string, $this->offset, 1); } public function key(): int { return $this->offset; } public function next(): void { ++$this->offset; } }
  • 38. Looping the Loop with SPL Iterators Implementing an Iterator $stringIterator = new Utf8StringIterator('Καλησπέρα σε όλους'); foreach($stringIterator as $character) { echo "[{$character}]"; }
  • 39. Looping the Loop with SPL Iterators Implementing an Iterator [Κ][α][λ][η][σ][π][έ][ρ][α][ ][σ][ε][ ][ό][λ][ο][υ][ς]
  • 40. Looping the Loop with SPL Iterators Implementing an Iterator class FileLineIterator implements Iterator { private $fileHandle; private $line = 1; public function __construct(string $fileName) { $this->fileHandle = fopen($fileName, 'r'); } public function __destruct() { fclose($this->fileHandle); } }
  • 41. Looping the Loop with SPL Iterators Implementing an Iterator class FileLineIterator implements Iterator { ... public function current(): string { $fileData = fgets($this->fileHandle); return rtrim($fileData, "n"); } public function key(): int { return $this->line; } public function next(): void { ++$this->line; } }
  • 42. Looping the Loop with SPL Iterators Implementing an Iterator class FileLineIterator implements Iterator { ... public function rewind(): void { fseek($this->fileHandle, 0); $this->line = 1; } public function valid(): bool { return !feof($this->fileHandle); } }
  • 43. Looping the Loop with SPL Iterators Implementing an Iterator $fileLineIterator = new FileLineIterator(__FILE__); foreach($fileLineIterator as $lineNumber => $fileLine) { echo "{$lineNumber}: {$fileLine}", PHP_EOL; }
  • 44. Looping the Loop with SPL Iterators Implementing an Iterator 1: <?php 2: 3: class FileLineIterator implements Iterator { 4: private $fileHandle; 5: private $line = 1; 6: 7: public function __construct(string $fileName) { 8: $this->fileHandle = fopen($fileName, 'r’); 9: } 10: …
  • 45. Looping the Loop with SPL Iterators
  • 46. Looping the Loop with SPL Iterators class UserRepository { // ... public function all(): array { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); $statement->execute(); $users = $statement->fetchAll(); return array_map(fn(array $user): User => User::fromArray($user), $users); } }
  • 47. Looping the Loop with SPL Iterators class UserRepository { // ... /** * @return User[] */ public function all(): array { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); $statement->execute(); $users = $statement->fetchAll(); return array_map(fn(array $user): User => User::fromArray($user), $users); } }
  • 48. Looping the Loop with SPL Iterators class UserCollection extends ArrayIterator { }
  • 49. Looping the Loop with SPL Iterators class UserRepository { // ... public function all(): UserCollection { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); $statement->execute(); $users = $statement->fetchAll(); return new UserCollection( array_map(fn(array $user): User => User::fromArray($user), $users) ); } }
  • 50. Looping the Loop with SPL Iterators class UserCollection extends ArrayIterator { public function current(): User { return User::fromArray(parent::current()); } }
  • 51. Looping the Loop with SPL Iterators class UserRepository { // ... public function all(): UserCollection { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); $statement->execute(); $users = $statement->fetchAll(); return new UserCollection($users); } }
  • 52. Looping the Loop with SPL Iterators
  • 53. Looping the Loop with SPL Iterators ArrayIterator Implements: Countable ArrayAccess Serializable SeekableIterator (which extends Iterator)
  • 54. Looping the Loop with SPL Iterators ArrayIterator Implements: Countable
  • 55. Looping the Loop with SPL Iterators ArrayIterator – Countable interface Countable { /* Methods */ abstract public count ( void ) : int }
  • 56. Looping the Loop with SPL Iterators ArrayIterator – Countable $userData = ['user1', 'user2', 'user3', 'user4']; $userList = new UserList($userData); $userCount = count($userList); echo "There are {$userCount} users in this list", PHP_EOL;
  • 57. Looping the Loop with SPL Iterators ArrayIterator – Countable $userData = ['user1', 'user2', 'user3', 'user4']; $userList = new UserList($userData); echo "There are {$userList->count()} users in this list", PHP_EOL;
  • 58. Looping the Loop with SPL Iterators ArrayIterator – Countable There are 4 users in this list
  • 59. Looping the Loop with SPL Iterators ArrayIterator Implements: Countable ArrayAccess
  • 60. Looping the Loop with SPL Iterators ArrayIterator – ArrayAccess interface ArrayAccess { /* Methods */ abstract public offsetExists ( mixed $offset ) : bool abstract public offsetGet ( mixed $offset ) : mixed abstract public offsetSet ( mixed $offset , mixed $value ) : void abstract public offsetUnset ( mixed $offset ) : void }
  • 61. Looping the Loop with SPL Iterators ArrayIterator – ArrayAccess $userData = ['user1', 'user2', 'user3', 'user4']; $userList = new UserList($userData); echo "The 3rd entry in the list is {$userList[2]}", PHP_EOL;
  • 62. Looping the Loop with SPL Iterators ArrayIterator – ArrayAccess The 3rd entry in the list is user3
  • 63. Looping the Loop with SPL Iterators ArrayIterator Implements: Countable ArrayAccess Serializable
  • 64. Looping the Loop with SPL Iterators ArrayIterator – Serializable interface Serializable { /* Methods */ abstract public serialize ( void ) : string abstract public unserialize ( string $serialized ) : void }
  • 65. Looping the Loop with SPL Iterators ArrayIterator – Serializable $userData = ['user1', 'user2', 'user3', 'user4']; $userList = new UserList($userData); var_dump(serialize($userList));
  • 66. Looping the Loop with SPL Iterators ArrayIterator – Serializable string(117) "O:8:"UserList":4:{i:0;i:0;i:1;a:4:{i:0;s:5:"user1"; i:1;s:5:"user2";i:2;s:5:"user3";i:3;s:5:"user4";}i:2; a:0:{}i:3;N;}"
  • 67. Looping the Loop with SPL Iterators ArrayIterator Implements: Countable ArrayAccess Serializable SeekableIterator (which extends Iterator)
  • 68. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator SeekableIterator extends Iterator { /* Methods */ abstract public seek ( int $position ) : void /* Inherited methods */ abstract public Iterator::current ( void ) : mixed abstract public Iterator::key ( void ) : scalar abstract public Iterator::next ( void ) : void abstract public Iterator::rewind ( void ) : void abstract public Iterator::valid ( void ) : bool }
  • 69. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator $userData = ['user1', 'user2', 'user3', 'user4']; $userList = new UserList($userData); $userList->seek(2); while ($userList->valid()) { echo $userList->current(), PHP_EOL; $userList->next(); }
  • 70. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator user3 user4
  • 71. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator $userData = ['user1', 'user2', 'user3', 'user4']; $userList = new UserList($userData); $userList->seek(2); foreach($userList as $userName) { echo $userName, PHP_EOL; }
  • 72. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator user1 user2 user3 user4 
  • 73. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator $userData = ['A' => 'user1', 'B' => 'user2', 'C' => 'user3', 'D' => 'user4']; $userList = new UserList($userData); $userList->seek('C'); while ($userList->valid()) { echo $userList->current(), PHP_EOL; $userList->next(); }
  • 74. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator PHP7 Warning: ArrayIterator::seek() expects parameter 1 to be int, string given 
  • 75. Looping the Loop with SPL Iterators ArrayIterator – SeekableIterator PHP8 Fatal error: Uncaught TypeError ArrayIterator::seek(): Argument #1 ($position) must be of type int, string given 
  • 76. Looping the Loop with SPL Iterators ArrayIterator Implements: Countable ArrayAccess Serializable SeekableIterator (which extends Iterator) Also provides methods for sorting Doesn’t work with array_* functions 
  • 77. Looping the Loop with SPL Iterators
  • 78. Looping the Loop with SPL Iterators
  • 79. Looping the Loop with SPL Iterators LimitIterator $userData = ['user1', 'user2', 'user3', 'user4']; $userList = new LimitIterator( new UserList($userData), 2, 1 ); foreach($userList as $value) { echo $value, PHP_EOL; }
  • 80. Looping the Loop with SPL Iterators LimitIterator user3
  • 81. Looping the Loop with SPL Iterators InfiniteIterator $weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat']; $todayOffset = (int) (new DateTime('2020-09-08'))->format('w'); $weekdayList = new LimitIterator( new InfiniteIterator( new ArrayIterator($weekdayNames) ), $todayOffset, 14 ); foreach($weekdayList as $dayOfWeek) { echo $dayOfWeek, PHP_EOL; }
  • 82. Looping the Loop with SPL Iterators InfiniteIterator Tues Wed Thurs Fri Sat Sun Mon Tues Wed Thurs Fri Sat Sun Mon
  • 83. Looping the Loop with SPL Iterators CallbackFilterIterator $weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat']; $todayOffset = (int) (new DateTime('2020-09-15'))->format('w'); $workdayList = new CallbackFilterIterator( new LimitIterator( new InfiniteIterator( new ArrayIterator($weekdayNames) ), $todayOffset, 14 ), fn($dayName): bool => $dayName !== 'Sat' && $dayName !== 'Sun' ); foreach($workdayList as $dayOfWeek) { echo $dayOfWeek, PHP_EOL; }
  • 84. Looping the Loop with SPL Iterators CallbackFilterIterator Tues Wed Thurs Fri Mon Tues Wed Thurs Fri Mon
  • 85. Looping the Loop with SPL Iterators
  • 86. Looping the Loop with SPL Iterators FilterIterator abstract FilterIterator extends IteratorIterator implements OuterIterator { /* Methods */ public abstract accept ( void ) : bool public __construct ( Iterator $iterator ) public current ( void ) : mixed public getInnerIterator ( void ) : Iterator public key ( void ) : mixed public next ( void ) : void public rewind ( void ) : void public valid ( void ) : bool }
  • 87. Looping the Loop with SPL Iterators FilterIterator class ImageFileIterator extends FilterIterator { // Overwrite the constructor to automagically create an inner iterator // to iterate the file system when given a path name. public function __construct(string $path) { parent::__construct(new FilesystemIterator($path)); } // Overwrite the accept method to perform a super simple test to // determine if the files found were images or not. public function accept(): bool { $fileObject = $this->getInnerIterator()->current(); return preg_match('/^(?:gif|jpe?g|png)$/i', $fileObject->getExtension()); } }
  • 88. Looping the Loop with SPL Iterators FilterIterator class ImageFileIterator extends FilterIterator { // Overwrite the constructor to automagically create an inner iterator // to iterate the file system when given a path name. public function __construct(string $path) { parent::__construct(new FilesystemIterator($path)); } // Overwrite the accept method to perform a super simple test to // determine if the files found were images or not. public function accept(): bool { return preg_match('/^(?:gif|jpe?g|png)$/i', $this->getExtension()); } }
  • 89. Looping the Loop with SPL Iterators FilterIterator $path = dirname(__FILE__); foreach(new ImageFileIterator($path) as $imageObject) { echo $imageObject->getBaseName(), PHP_EOL; }
  • 90. Looping the Loop with SPL Iterators FilterIterator anubis.jpg Carpet.jpg Crazy Newspaper Headlines.jpg elephpant.jpg Pie Chart.jpg PowerOfBooks.jpg Struggling-Team.png TeddyBears.jpg
  • 91. Looping the Loop with SPL Iterators Iterator Keys class ImageFileIterator extends FilterIterator { ... // Overwrite the key method to return the file extension as the key public function key(): string { return $this->getExtension(); } } foreach(new ImageFileIterator($path) as $extension => $imageObject) { echo "{$extension} => {$imageObject->getBaseName()}", PHP_EOL; }
  • 92. Looping the Loop with SPL Iterators Iterator Keys jpg => anubis.jpg jpg => Carpet.jpg jpg => Crazy Newspaper Headlines.jpg jpg => elephpant.jpg jpg => Pie Chart.jpg jpg => PowerOfBooks.jpg png => Struggling-Team.png jpg => TeddyBears.jpg
  • 93. Looping the Loop with SPL Iterators Iterator Keys class ImageFileIterator extends FilterIterator { ... // Overwrite the key method to return the last modified date/time as the key public function key(): DateTime { return DateTime::createFromFormat('U', $this->getMTime()); } } foreach(new ImageFileIterator($path) as $date => $imageObject) { echo "{$date->format('Y-m-d H:i:s')} => {$imageObject->getBaseName()}", PHP_EOL; }
  • 94. Looping the Loop with SPL Iterators Iterator Keys 2012-03-24 00:38:24 => anubis.jpg 2013-01-27 22:32:20 => Carpet.jpg 2013-03-07 12:16:24 => Crazy Newspaper Headlines.jpg 2012-02-16 22:31:56 => elephpant.jpg 2012-10-09 11:51:56 => Pie Chart.jpg 2012-07-17 12:22:32 => PowerOfBooks.jpg 2012-12-30 18:14:24 => Struggling-Team.png 2012-01-31 11:17:32 => TeddyBears.jpg
  • 95. Looping the Loop with SPL Iterators
  • 96. Looping the Loop with SPL Iterators MultipleIterator $dates = ['Q1-2019', 'Q2-2019', 'Q3-2019', 'Q4-2019']; $budget = [1200, 1300, 1400, 1500]; $expenditure = [1250, 1315, 1440, 1485]; $budgetElements = count($budget); for ($i = 0; $i < $budgetElements; $i++) { echo "{$dates[$i]} - {$budget[$i]}, {$expenditure[$i]}", PHP_EOL; }
  • 97. Looping the Loop with SPL Iterators MultipleIterator $dates = ['Q1-2019', 'Q2-2019', 'Q3-2019', 'Q4-2019']; $budget = [1200, 1300, 1400, 1500]; $expenditure = [1250, 1315, 1440, 1485]; foreach($dates as $key => $period) { echo "{$period} - {$budget[$key]}, {$expenditure[$key]}", PHP_EOL; }
  • 98. Looping the Loop with SPL Iterators MultipleIterator Q1-2019 - 1200, 1250 Q2-2019 - 1300, 1315 Q3-2019 - 1400, 1440 Q4-2019 - 1500, 1485
  • 99. Looping the Loop with SPL Iterators MultipleIterator function wrapArrayAsIterator(array $array) { return new ArrayIterator($array); } $combinedIterable = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); $combinedIterable->attachIterator(wrapArrayAsIterator($dates), 'date'); $combinedIterable->attachIterator(wrapArrayAsIterator($budget), 'budget'); $combinedIterable->attachIterator(wrapArrayAsIterator($expenditure), 'expenditure');
  • 100. Looping the Loop with SPL Iterators MultipleIterator foreach($combinedIterable as $dateValues) { echo $dateValues['date'], " - {$dateValues['budget']}, {$dateValues['expenditure']}", PHP_EOL; }
  • 101. Looping the Loop with SPL Iterators MultipleIterator foreach($combinedIterable as $dateValues) { [$date, $budget, $expenditure] = $dateValues; echo "{$date} - {$budget}, {$expenditure}", PHP_EOL; }
  • 102. Looping the Loop with SPL Iterators MultipleIterator Q1-2019 - 1200, 1250 Q2-2019 - 1300, 1315 Q3-2019 - 1400, 1440 Q4-2019 - 1500, 1485
  • 103. Looping the Loop with SPL Iterators MultipleIterator function wrapArrayAsIterator(array $array) { return new ArrayIterator($array); } $combinedIterable = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); $combinedIterable->attachIterator(wrapArrayAsIterator($dates), 'date'); $combinedIterable->attachIterator(wrapArrayAsIterator($budget), 'budget'); $combinedIterable->attachIterator(wrapArrayAsIterator($expenditure), 'expenditure');
  • 104. Looping the Loop with SPL Iterators MultipleIterator foreach($combinedIterable as $dateValues) { extract($dateValues); echo "{$date} - {$budget}, {$expenditure}", PHP_EOL; }
  • 105. Looping the Loop with SPL Iterators MultipleIterator Q1-2019 - 1200, 1250 Q2-2019 - 1300, 1315 Q3-2019 - 1400, 1440 Q4-2019 - 1500, 1485
  • 106. Looping the Loop with SPL Iterators MultipleIterator $allocations = $totalAdvance->allocateTo(count($cars)); foreach ($cars as $i => $car) { $car->setAdvance($allocations[$i]); }
  • 107. Looping the Loop with SPL Iterators MultipleIterator $allocations = $totalAdvance->allocateTo(count($cars)); $advanceAllocations = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); $advanceAllocations->attachIterator(ArrayIterator($cars), 'car'); $advanceAllocations->attachIterator(ArrayIterator($allocations), 'advance'); foreach($advanceAllocations as $advanceAllocation) { extract($advanceAllocation); $car->setAdvance($advance); }
  • 108. Looping the Loop with SPL Iterators
  • 109. Looping the Loop with SPL Iterators Inner and Outer Iterators Inner Iterator Implements Iterator, or extends a class that implements Iterator ArrayIterator SeekableIterator FilesystemIterator MultipleIterator
  • 110. Looping the Loop with SPL Iterators Inner and Outer Iterators Outer Iterator Implements OuterIterator, or extends an Iterator that already implements OuterIterator LimitIterator InfiniteIterator FilterIterator CallbackFilterIterator
  • 111. Looping the Loop with SPL Iterators Recursive Iterators A RecursiveIterator defines methods to allow iteration over hierarchical data structures. Nested Arrays (and Objects if using ArrayAccess) Filesystem Directories/Folders
  • 112. Looping the Loop with SPL Iterators
  • 113. Looping the Loop with SPL Iterators class UserRepository { // ... public function all(): UserCollection { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); $statement->execute(); $users = $statement->fetchAll(); return new UserCollection( array_map(fn(array $user): User => User::fromArray($user), $users) ); } }
  • 114. Looping the Loop with SPL Iterators class UserRepository { // ... public function all(): Generator { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); $statement->execute(); while ($user = $statement->fetch()) { yield User::fromArray($user); } } }
  • 115. Looping the Loop with SPL Iterators class UserRepository { // ... /** * @return Generator|User[] */ public function all(): Generator { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); $statement->execute(); while ($user = $statement->fetch()) { yield User::fromArray($user); } } }
  • 116. Looping the Loop with SPL Iterators IteratorAggregate IteratorAggregate extends Traversable { /* Methods */ abstract public getIterator ( void ) : Traversable }
  • 117. Looping the Loop with SPL Iterators class UserCollection implements IteratorAggregate { private PDOStatement $pdoStatement; public function __construct(PDOStatement $pdoStatement) { $this->pdoStatement = $pdoStatement; } public function getIterator(): Traversable { $this->pdoStatement->execute(); while ($user = $this->pdoStatement->fetch()) { yield User::fromArray($user); } } }
  • 118. Looping the Loop with SPL Iterators class UserRepository { // ... public function all(): UserCollection { $userQuery = 'SELECT * FROM `users` ...'; $statement = $this->pdo->prepare($userQuery); return new UserCollection($statement); } }
  • 119. Looping the Loop with SPL Iterators Implementing a Fluent Nested Iterator $weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat']; $todayOffset = (int) (new DateTime('2020-09-15'))->format('w'); $workdayList = new CallbackFilterIterator( new LimitIterator( new InfiniteIterator( new ArrayIterator($weekdayNames) ), $todayOffset, 14 ), fn($dayName): bool => $dayName !== 'Sat' && $dayName !== 'Sun' ); foreach($workdayList as $dayOfWeek) { echo $dayOfWeek, PHP_EOL; }
  • 120. Looping the Loop with SPL Iterators Implementing a Fluent Nested Iterator class FluentNestedIterator implements IteratorAggregate { protected iterable $iterator; public function __construct(iterable $iterator) { $this->iterator = $iterator; } public function with(string $iterable, ...$args): self { $this->iterator = new $iterable($this->iterator, ...$args); return $this; } public function getIterator() { return $this->iterator; } }
  • 121. Looping the Loop with SPL Iterators Implementing a Fluent Nested Iterator $weekdayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat']; $todayOffset = (int) (new DateTime('2020-10-08'))->format('w'); $workdayList = (new FluentNestedIterator(new ArrayIterator($weekdayNames))) ->with(InfiniteIterator::class) ->with(LimitIterator::class, $todayOffset, 14) ->with( CallbackFilterIterator::class, fn($dayName): bool => $dayName !== 'Sat' && $dayName !== 'Sun' ); foreach($workdayList as $dayOfWeek) { echo $dayOfWeek, PHP_EOL; }
  • 122. Looping the Loop with SPL Iterators Implementing a Fluent Nested Iterator Thurs Fri Mon Tues Wed Thurs Fri Mon Tues Wed
  • 123. Looping the Loop with SPL Iterators
  • 124. Looping the Loop with SPL Iterators Array Functions Don’t work with Traversables 
  • 125. Looping the Loop with SPL Iterators Array Functions Don’t work with Traversables 
  • 126. Looping the Loop with SPL Iterators Array Functions – Filter function filter(iterable $iterator, Callable $callback = null) { if ($callback === null) { $callback = 'iterableFilterNotEmpty'; } foreach ($iterator as $key => $value) { if ($callback($value)) { yield $key => $value; } } }
  • 127. Looping the Loop with SPL Iterators Array Functions – Filter $startTime = new DateTime('2015-11-23 13:20:00Z'); $endTime = new DateTime('2015-11-23 13:30:00Z'); $timeFilter = function($timestamp) use ($startTime, $endTime) { return $timestamp >= $startTime && $timestamp <= $endTime; }; $filteredTrackingData = filter( $gpxReader->getElements('trkpt'), $timeFilter, ARRAY_FILTER_USE_KEY ); foreach ($filteredTrackingData as $time => $element) { ... }
  • 128. Looping the Loop with SPL Iterators Array Functions – Filter 2015-11-23 13:24:40 latitude: 53.5441 longitude: -2.7416 elevation: 124 2015-11-23 13:24:49 latitude: 53.5441 longitude: -2.7416 elevation: 122 2015-11-23 13:24:59 latitude: 53.5441 longitude: -2.7416 elevation: 120 2015-11-23 13:25:09 latitude: 53.5441 longitude: -2.7417 elevation: 120 2015-11-23 13:25:19 latitude: 53.5441 longitude: -2.7417 elevation: 121 2015-11-23 13:25:29 latitude: 53.5441 longitude: -2.7417 elevation: 120 2015-11-23 13:25:39 latitude: 53.5441 longitude: -2.7417 elevation: 120
  • 129. Looping the Loop with SPL Iterators Array Functions – Filter Of course, we could always use a CallBackFilterIterator
  • 130. Looping the Loop with SPL Iterators Array Functions – Map function map(Callable $callback, iterable $iterator) { foreach ($iterator as $key => $value) { yield $key => $callback($value); } }
  • 131. Looping the Loop with SPL Iterators Array Functions – Map $distanceCalculator = new GpxReaderHelpersDistanceCalculator(); $mappedTrackingData = map( [$distanceCalculator, 'setDistanceProperty'], $gpxReader->getElements('trkpt') ); foreach ($mappedTrackingData as $time => $element) { ... }
  • 132. Looping the Loop with SPL Iterators Array Functions – Map 2015-11-23 14:57:07 latitude: 53.5437 longitude: -2.7424 elevation: 103 distance from previous point: 6.93 m 2015-11-23 14:57:17 latitude: 53.5437 longitude: -2.7424 elevation: 100 distance from previous point: 1.78 m 2015-11-23 14:57:27 latitude: 53.5438 longitude: -2.7425 elevation: 89 distance from previous point: 11.21 m 2015-11-23 14:57:37 latitude: 53.5439 longitude: -2.7424 elevation: 83 distance from previous point: 9.23 m 2015-11-23 14:57:47 latitude: 53.5439 longitude: -2.7425 elevation: 92 distance from previous point: 5.40 m
  • 133. Looping the Loop with SPL Iterators Array Functions – Walk iterator_apply() iterator_apply( Traversable $iterator, Callable $callback [, array $args = NULL ] ) : int
  • 134. Looping the Loop with SPL Iterators Array Functions – Reduce function reduce(iterable $iterator, Callable $callback, $initial = null) { $result = $initial; foreach($iterator as $value) { $result = $callback($result, $value); } return $result; }
  • 135. Looping the Loop with SPL Iterators Array Functions – Reduce $distanceCalculator = new GpxReaderHelpersDistanceCalculator(); $totalDistance = reduce( map( [$distanceCalculator, 'setDistanceProperty'], $gpxReader->getElements('trkpt') ), function($runningTotal, $value) { return $runningTotal + $value->distance; }, 0.0 );
  • 136. Looping the Loop with SPL Iterators Array Functions – Reduce Total distance travelled is 4.33 km
  • 137. Looping the Loop with SPL Iterators Array Functions – Other Iterator Functions Iterator_count() iterator_count ( Traversable $iterator ) : int
  • 138. Looping the Loop with SPL Iterators Array Functions – Other Iterator Functions Iterator_to_array() iterator_to_array ( Traversable $iterator [, bool $use_keys = TRUE ] ) : array
  • 139. Looping the Loop with SPL Iterators
  • 140. Looping the Loop with SPL Iterators A Functional Guide to Cat Herding with PHP Generators • https://markbakeruk.net/2016/01/19/a-functional-guide-to-cat-herding-with-php-generators/ • https://markbakeruk.net/2020/01/05/filtering-and-mapping-with-spl-iterators/ • https://markbakeruk.net/2019/12/31/parallel-looping-in-php-with-spls-multipleiterator/
  • 141. Looping the Loop with SPL Iterators Iterating PHP Iterators By Cal Evans https://leanpub.com/iteratingphpiterators by Joshua Thijssen https://www.phparch.com/books/mastering-the-spl-library/
  • 142. Who am I? Mark Baker Senior Software Engineer Recharge.com, Amsterdam Coordinator and Developer of: Open Source PHPOffice library PHPSpreadsheet, PHPWord, PHPPresentation, PHPProject, PHPVisio Minor contributor to PHP core (SPL DataStructures) Other small open source libraries available on github @Mark_Baker https://github.com/MarkBaker http://uk.linkedin.com/pub/mark-baker/b/572/171 http://markbakeruk.net Looping the Loop with SPL Iterators