Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering PHP 7

You're reading from   Mastering PHP 7 Design, configure, build, and test professional web applications

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781785882814
Length 536 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Branko Ajzele Branko Ajzele
Author Profile Icon Branko Ajzele
Branko Ajzele
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. The All New PHP FREE CHAPTER 2. Embracing Standards 3. Error Handling and Logging 4. Magic Behind Magic Methods 5. The Realm of CLI 6. Prominent OOP Features 7. Optimizing for High Performance 8. Going Serverless 9. Reactive Programming 10. Common Design Patterns 11. Building Services 12. Working with Databases 13. Resolving Dependencies 14. Working with Packages 15. Testing the Important Bits 16. Debugging, Tracing, and Profiling 17. Hosting, Provisioning, and Deployment

Generator delegation

Iterating through a list of items is among the most common things in any programming language. PHP makes it easy to iterate over a diverse collection of data using the foreach construct. Many languages differentiate various data types of collection data, such as dictionary, list, set, tuple, and alike. PHP, however, does not dwell that much on data structures and simply uses the array() or [] constructs most of the time for its collections. This, in turn, can have a negative impact on creating large arrays in memory, which could cause exceeding memory limits or even increased processing times.

Aside from the primitive array type, PHP also provides the ArrayObject and ArrayIterator classes. These turn arrays into a first class citizens in an OOP application.

Generators allow us to write code that uses foreach to iterate over a set of data without needing to build an array. They are like a function that yields as many values as needed, instead of returning just one, which gives them an iterator-like behavior. While generators have been around from PHP 5.5, they lacked more advanced functionality. Generator delegation is one of the improvements made available with the release of PHP 7.

Let's take a look at the following example:

function even() {
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
yield $i;
}
}
}

function odd() {
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 != 0) {
yield $i;
}
}
}

function mix() {
yield -1;
yield from odd();
yield 17;
yield from even();
yield 33;
}

// 2 4 6 8 1 0
foreach (even() as $even) {
echo $even;
}

// 1 3 5 7 9
foreach (odd() as $odd) {
echo $odd;
}

// -1 1 3 5 7 9 17 2 4 6 8 10 33
foreach (mix() as $mix) {
echo $mix;
}

Here, we define three generator functions: even, odd, and mix. The mix function demonstrates the concept of generator delegation via the use of yield from <expr>. Whereas, <expr> is any expression that evaluates to a traversable object or array. We can see that the result of looping through the mix function echoes all of the yielded values from both itself as well as the even and odd functions.

The generator delegation syntax allows the factoring of yield statements into smaller conceptual units, giving generators the similar organizational functionality as methods give to classes. Used carefully, this can improve our code quality and readability.

You have been reading a chapter from
Mastering PHP 7
Published in: Jun 2017
Publisher: Packt
ISBN-13: 9781785882814
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime