Lightweight "collection" iterator-generator

Source: Internet
Author: User
Tags what php

Generator is a new language feature added to PHP 5.5. However, it does not seem to be widely used by many PHP developers. So before we learn about PHP 7 's improvements to generator, let's start with a simple but obvious example of what the next generator is going to do to solve the problem for us.

An array of 10 million integers

What if we want to "create an array of the specified number of elements"? We may not be able to write down the following code:

function makeRange($range) { $data = []; for ($i = 0; $i < $range; $i++) { $data[] = $i; } return $data;}

Then, in our development environment, create an array of 10,000, 100,000, and 1 million elements, each of which looks as we imagined.

makeRange(10000);makeRange(100000);makeRange(1000000);

However, when we create an array with 10 million integers, the situation is different, and the system does not have that much memory allocated to us:

makeRange(10000000);

Get PHP generator out and help.

Defining PHP Generator is simple and looks like defining a function:

function makeRangeByGenerator($range) { for ($i = 0; $i < $range; $i++) { yield $i; }}

Simply, Makerangebygenerator does not have a return value and does not create the entire array in memory, but only the keyword yield, which marks the value that should be generated for Each loop. Next, we can use generator like accessing a normal collection:

foreach ($makeRangeByGenerator(100) as $i) { echo $i.‘<br>‘;}

Now that we re-execute our PHP file, we won't be able to get an error. And, on the page, we can see the value generated by the generator. This is the typical application scenario for generator, simply put: generator is a lightweight iterator that automatically remembers the state of each call and returns to us the correct value.
What improvements did PHP 7 make?

Now that we understand the basic usage of generator, we can look at what PHP 7 has done to improve it.

    • PHP 7 allows us to add a return value to generator, just as we define the function's return value:

      function Makerangebygenerator ($range) {

         for ($i = 0; $i < $range; $i++) { yield $i; } return "Finish yielding";

      }

When iterating through all the elements of generator, we can read the return value of the generator through the Getreturn () method:

$gen = makeRangeByGenerator(100);foreach ($gen as $i) { echo $i.‘<br>‘;}echo $gen->getReturn();

*"We have to generator all the values in the iteration to read the return value of the generator, otherwise PHP will error." ”
--Best Practices *

    • PHP 7 A second improvement to generator is to allow nesting. For example:

function outer() {    yield "PHP 7 "; yield "is one of "; yield "the best "; yield from inner();}function inner() { yield "programming languages in the world";}

We use the keyword from to introduce a new generator, and when we traverse the outer generator, we find that it automatically generates the value of the inner generator:

foreach(outer() as $str) {    echo $str;}
    • PHP 7 A second improvement to generator is to allow nesting. For example:

function outer() {    yield "PHP 7 "; yield "is one of "; yield "the best "; yield from inner();}function inner() { yield "programming languages in the world";}

We use the keyword from to introduce a new generator, and when we traverse the outer generator, we find that it automatically generates the value of the inner generator:

foreach(outer() as $str) {    echo $str;}

This is the entire content of PHP generator. Simply put, generator is a lightweight, "set" iterator that remembers its own state. If you don't know it before, it's time to think about where it can help you.

Learn more about high-quality IT technology, expand your reading horizons, welcome to visit our partners Segmentfault

Lightweight "collection" iterator-generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.