PHP iterators and Kazuo Generators

Source: Internet
Author: User

PHP has a number of powerful interfaces, where arrayaccess and Iterator work together to make objects as flexible as arrays.

Of course, using arrayaccess with Iterator can be used to deal with arrays, but there is a better way to do this with the arrayiterator provided by SPL

The reason is:

Arrayiterator implement Arrayaccess, Seekableiterator, countable, searializable {}

The next step is to introduce a higher level of iterator usage.

Functions related to iterator are recorded first.

Iterator_to_array () Converts an element in an iterator to an array

Iteratoraggregate::getiterator () calls an external iterator

Arrayiterator

Iteartor_count () et cetera

PHP's use of the iterator interface is an implementation of the iterator pattern. Here, the conceptual client (implementing the iterative process), the iterator, and the specific iterator do not correspond to, foreach (), the concrete class that inherits from the iterator interface, and the array or collection that needs to be traversed.

The generator, however, is built on the basis of an understanding iterator.

The generator in PHP can be called an iterative generator because it is a class that is not new, inherits iterator, and has one more send () to communicate with the generator.

Its implementation is through the yield keyword, or statement, or expression, which works by using the yield of the struct is a generation generator class, when executed to yield

, the generator is interrupted and its state is stored, and when it is executed again (a loop structure such as foreach or while), it resumes its state and until the yield is encountered again

 <?phpfunction Gen () {$ret = (yield ' yield1 ');    Var_dump ($ret);    $ret = (yield ' yield2 '); Var_dump ($ret);}    $gen = Gen (); Var_dump ($gen->current ()); Output:string (6) "Yield1" when the generator is formed//rewind () has been implicitly executed, that is, the generation has been to the first yield interrupted var_dump ($gen-&G T;send (' Ret1 ')); Output:string (4) "Ret1" (the first var_dump in Gen)//At this point, send () did what it had to do, resumed the interrupt, and passed the value (ret1) to Y Ield, and returns yield (RET1) until yield is encountered, NONE returns null//Output:string (6) "Yield2" (the Var_dump of the- >send () return value)//Then execute to $ret = (yields ' yield2 '); In the core, and return the value of the yield expression, 
//At this time is yield2, if there is no subsequent ' yield2 ', it will return Nullvar_dump ($gen->send (' ret 2 ')); Output:string (6) "Yield2" (the Var_dump of the->send () return value)
//Output:nul L
//When send () is executed, there is no next yield to return null, and after its recovery executes there is a var_dump () in the function body, so there will be output

In fact, from the above example can be summed up two points,

First, when the generator is initialized, it has reached a yield, resulting in an interrupt,

Second, the execution of send is actually, first next (), then vaild (), cannot pass the return null, pass the current (), return, if yield has no "default" ($ret = (yield ' defaults '),

The null is returned and then interrupted until it is resumed again.

Understanding how it works, there is a real problem, what's the use of it?

The high-level use of the generator appears in the topic "Using the coprocessor to implement multitasking in PHP", which is difficult, and my understanding of it is only a simple task scheduler.

And more advanced content, and then slowly understand.

Why does it accomplish the task of scheduling? This can be analogous to a program interrupt in the operating system, where the function of the interrupt is for task scheduling.

How to use yield to complete task scheduling, this

PHP iterators and Kazuo Generators

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.