Lightweight "collection" iterator-generator
Park video link
Parking Document Links
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);
In the case of limited resources, it is a typical application of PHP generator to solve the problem of large file or large volume data processing.
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 *
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
Park video link
Parking Document Links
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);
In the case of limited resources, it is a typical application of PHP generator to solve the problem of large file or large volume data processing.
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 *
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
Original:
1190000004307491
Get started and transition to PHP7 (5)-Lightweight "set" iterator-generator