Home  >  Article  >  Backend Development  >  php generator object

php generator object

伊谢尔伦
伊谢尔伦Original
2016-11-23 09:06:441492browse

When a generator function is called for the first time, it will return an object of the internal Generator class. This object implements the Iterator interface in almost the same way as the front iterator object.

Most of the methods in the Generator class have the same semantics as the methods in the Iterator interface, but the generator object has an additional method: send().

CautionGenerator objects cannot be instantiated through new

Example #1 The Generator class

Generator::send()

Generator::send() allows values ​​to be injected into generator methods when iterating. The injected value will be returned from the yield statement and then used in any generator method Used in variables.

Example #2 Using Generator::send() to inject values

send('Hello world!');
?>

The above routine will output:

Hello world!
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
Previous article:Use of PHP SPLNext article:Use of PHP SPL