Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.
  1. When is it best to use Iterators in PHP, and how can they be implemented to best avoid loading all objects into memory simultaneously?
  2. Do any constructs exist in PHP so that we can queue up results of an operation for use with an Iterator, while again avoiding loading all objects into memory simultaneously?
    1. An example would be a curl HTTP request against a REST server
  3. In the case of an HTTP request that returns all results at once (a la curl), would we be better off to go with streaming results, and if so, are there any limitations or pitfalls to be aware of?
    1. If using streaming, is it better to replace curl with a PHP native stream/socket?

My intention is to implement Iterators for a REST client, and separately a document ORM that I'm maintaining, but only if I can do so while gaining benefits from reduced memory usage, increased performance, etc. Thanks in advance for any responses :-)

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

When is it best to use Iterators in PHP

Use an iterator anytime you want to access a "collection" of data (like an array) but in an object oriented way. It is nice to be able to use object access but also perform a "foreach" on that same object.

Do any constructs exist in PHP so that we can queue up results of an operation for use with an Iterator, while again avoiding loading all objects into memory simultaneously?

Nothing intrinsic; however, you just need to write sort of a paginator. You don't load the collection object up with 10,000 objects. You load like 20 or 50 or 100, then you skip ahead when you've exhausted the current set.

The cool thing about using an object here is that all of this is happening behind the scenes. I mean, you have to program it; however, once it works and you become a consumer of this object, you never again have to worry about the internals. Just use it. Just add this to the list of benefits as well.

Another benefit is that you can now test these collections. Sure, you could theoretically test an array; however, an array can't exhibit behaviors like an object can. Plus with an iterator, you still get (most of) the benefits of a simple array.

...is it better to replace curl with a PHP native stream/socket?

If you use something that is streams compatible (like file_get_contents), and you use stream context, there is little that you can't do. In fact, since not everyone has cURL, this may be the more portable option.

share|improve this answer
    
Thank you for the thoughtful response. So for the use cases of an ORM or REST API, I assume you would need to issue a separate count request to know how many items are actually available for pagination by the iterator? –  Jon L. Mar 21 '12 at 20:34
    
For MySQL you can cheat (see: SQL_CALC_FOUND_ROWS), or you can just manually issue the second count request. Of course, with an HTTP Web Service API (notice I avoided the word REST), you could get away with returning the data and the pagination metadata in the same response -- in fact, you could return the pagination info via headers if you really wanted to get fancy, but this is not at all necessary. –  wilmoore Mar 22 '12 at 4:56
    
@wilmoore For a HTTP web service, you could implement the OData protocol, which specifies a standard for pagination. –  Daniel Lo Nigro Mar 22 '12 at 7:49
    
@Daniel15 sure, that is one way to go :) –  wilmoore Mar 22 '12 at 13:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.