Home > Article > Backend Development > php generator object
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!