Home  >  Article  >  Backend Development  >  PHP55 new feature yield generator

PHP55 new feature yield generator

WBOY
WBOYOriginal
2016-07-28 08:27:011125browse

php official document yield http://php.net/manual/zh/language.generators.overview.php

Reference: http://laravelacademy.org/post/4317.html

The generator provides a more An easy way to implement simple object iteration. Compared with defining a class to implement the Iterator interface, the performance overhead and complexity are greatly reduced.

Generators allow you to write code in a foreach block to iterate over a set of data without creating an array in memory, which would hit your memory limit or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and instead of a normal function returning only once, the generator can yield as many times as needed to generate the values ​​that need to be iterated over.

<code><span>$length</span> = <span>1000000</span>;
<span>//生成器</span><span>foreach</span> (yieldTest(<span>$length</span>) <span>as</span><span>$v1</span>) {
    <span>echo</span><span>$v1</span>;
}
<span><span>function</span><span>yieldTest</span><span>(<span>$length</span>)</span> {</span><span>for</span>(<span>$i</span> = <span>0</span>; <span>$i</span> < <span>$length</span>; <span>$i</span>++) {
        <span>yield</span><span>$i</span> . <span>'Camel'</span>;
    }
}

<span>/*
迭代器
以下的代码会抛出错误:
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /var/www/test/90/yield/demo01.php on line 38
*/</span><span>foreach</span>(iteratorTest(<span>$length</span>) <span>as</span><span>$v2</span>) {
    <span>echo</span><span>$v2</span>;
}
<span><span>function</span><span>iteratorTest</span><span>(<span>$length</span>)</span> {</span><span>$data</span> = [];
    <span>for</span>(<span>$i</span> = <span>0</span>; <span>$i</span> < <span>$length</span>; <span>$i</span>++) {
        <span>$data</span>[] = <span>$i</span> . <span>'Camel'</span>;
    }
    <span>return</span><span>$data</span>;
}</code>

If you want to use a specific method to calculate a large amount of data, such as operating Excel table data, the impact on performance will be even greater. At this time, we can use the generator to instantly calculate and produce subsequent values ​​without occupying valuable memory space and using less memory.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the yield generator, a new feature of php55, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn