 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Stanislav Malyshev's Blog: Ruby-like iterators in PHP
by Chris Cornutt January 28, 2010 @ 11:21:04
In this new post to his blog Stanislav Malyshev looks at creating some Ruby-like iterators as close as they can get in PHP.
I've started playing with Ruby recently, and one of the things that got my attention in Ruby were iterators. They are different inside from regular loops but work in a similar way, and looks like people (at least ones that write tutorials and code examples) like to use them.
He saw how one of the iterators worked - iterating over a Ruby hash - and wondered how difficult it'd be to write up something comparable in PHP. He creates a simple iterator, an array class to lay on top of it and an example of it in use. The use isn't as clean as the Ruby iterator, but it works similarly. He also includes a modification that lets you use ranges for what to return.
voice your opinion now!
ruby iterator hash array
Rob Young's Blog: Chunking Large Queries with Iterators in PHP
by Chris Cornutt October 07, 2009 @ 10:42:02
Since sometimes you just don't want all of the results of a query back at once, Rob Young has posted a solution of his own using the Iterators included with PHP as a part of the SPL. His solution is to wrap it in a ChunkedQueryIterator that handles the work behind the scenes.
When executing large queries it's usually best not to load the whole result set in one go. Memory isn't infinite and PHP isn't renowned for handling it very well. So the obvious answer is to chunk the large query in to lots of smaller queries. [...] We want something to which we can just provide a PDO object, an SQL query and the chunk size. We should then be able to iterate over the resulting object as though it were a single result set.
He includes two code snippets of it in action, but asks the question of his readers - "How do you handle large database queries?" - to get some feedback on other alternatives.
voice your opinion now!
chunk large query iterator pdo
Johannes Schluter's Blog: Searching through archive files
by Chris Cornutt September 08, 2009 @ 12:12:07
Recently Johannes Schluter wrote up a post on searching through archived files (like .tar.gz or .bzip) to perform a full-text search on their contents:
As said the app is about browsing the PHP manual. The manual is provided as tar.gz to the app and I wanted to have a fulltext search. For accessing the tar.gz content I'm using phar. Yes, phar is not only for phar files but can work on different kinds of archives (tar.gz, tar.bz2, zip), too.
In the code he uses Iterators and a PharData instance to open and search the contents of the given file. He explains how it all works, too, as well as mentioning a few places where it might need a bit of work.
voice your opinion now!
phar search archive tutorial iterator
DevShed: Using Directory Iterators to Build Loader Apps in PHP
by Chris Cornutt July 06, 2009 @ 10:17:11
DevShed finishes off their "loader" series of tutorials today with this eighth part focusing on the use of Directory Iterators.
Here's where the SPL comes in, since it's possible to use a combination of its "spl_autoload_register()" function and its RecursiveDirectoryIterator class to refactor the method in question and make it shorter and tighter. In this final chapter of the series I'm going to improve the loader class developed in the previous one by incorporating some of the aforementioned SPL functions and classes.
They change up their code to use a RecursiveDirectoryIterator inside of their __autoload to remove their custom recursive code.
voice your opinion now!
tutorial autoload recursive iterator directory spl
Johannes Schluter's Blog: MySQLi Resultset Iterator
by Chris Cornutt June 22, 2009 @ 11:12:11
Johannes Schluter has posted a look at a handy little script that shows an interface between the returned MySQLi results and an SPL iterator.
When using MySQLi's multi_query to send queries which return multiple result sets you have to use a rather unintuitive API which can certainly be improved. Recently I sat down and cooked up a small improvement for that, being an iterator fan I, of course, had to use an iterator for that and implemented the following class.
The class extends the standard Iterator and provides the interfaces to work through the results of the query in your choice of Iterator-supporting looping structure. Example code for the class and its usage are both included.
voice your opinion now!
iterator result mysqli
Rafael Dohms' Blog: SPL a hidden gem
by Chris Cornutt June 10, 2009 @ 11:19:06
Earlier this month Rafael Dohms posted a new article to his blog looking at a feature of PHP it seems not every developer knows about - the Standard PHP Library (or SPL).
By a show of hands, how many people here ever heard of SPL? How many already used it? Chances are most of you didn't raise your hands, and some might even have a confused look on their faces. Indeed that is the sad reality when it comes to SPL, but What is SPL?
He goes on to look at a few different things the SPL has to offer like autoloader overloading, iterators (with an included list of 21 of them) and the SplFixedArray that can be used to help speed up array access and manipulation.
voice your opinion now!
splfixedarray autoloader iterator spl
Procurios Blog: Syntactic Sugar for MySQLi Results using SPL Iterators
by Chris Cornutt May 15, 2009 @ 11:14:31
From the Procurios blog there's a recent post looking at a method letting you use a foreach on the results from a MySQLi request - SPL Iterators.
Ever wondered why you can't use foreach() on MySQLi Results, and instead have to write less convenient while() loops with fetch_row? Actually, you can use foreach() on MySQLi Results. All it takes is some SPL Iterator magic.
The code examples show how to create an Iterator interface (with rewind, current, key, next and valid methods) to create a ResultIterator class for moving back and forth between the values in the result. This allows you to define the new Iterator object and use the foreach structure like you would a normal result set.
They also show how to bypass this whole problem by using a IteratorAggregate in an extension of the MySQLi interface.
voice your opinion now!
resultset mysqli spl tutorial iterator
Blue Parabola Blog: The SPL Deserves Some Reiteration
by Chris Cornutt February 27, 2009 @ 08:45:35
On the Blue Parabola blog Matthew Turland has written up a post about a PHP something that might need a bit more love - the Standard PHP Library.
If any PHP extension is underrated, it's probably the SPL (Standard PHP Library). From what I can tell without having been involved in its development, its purpose is somewhat similar to the STL. A while back, it was useful mainly for allowing class instances to be iterable and simulate array access. [...] Using the SPL classes actually turned out to be pretty straightforward once you got your hands on a good starting guide or two to help you beat the learning curve.
He mentions a few of the things offered by the SPL like the SplFixedArray/SplFastArray, Iterators, SplDoublyLinkedList, SplStack and SplQueue (among others). He also includes some benchmarks running standard code (like normal arrays) against a SPL counterpart - the SPL side beat the normal side hands down. Check out the full post for all of the numbers and comparison types.
voice your opinion now!
spl standard library iterator array queue list stack benchmark
Timothy Boronczyk's Blog: Evil Access (a Database Class)
by Chris Cornutt January 21, 2009 @ 21:34:04
Timothy Boronczyk has posted a class that provides a different sort of theory on database access:
I was thinking today about database APIs when inspiration struck. I ended up hacking out the following class, which I think demonstrates a rather interesting approach to interfacing with a database (interesting enough at least to post here).
His class implements an Iterator and lets you bend a few of the rules PHP normally has in place (use of the magic methods, special characters in an identifier). He also includes an example of its use - connecting to the database, selecting information and pulling that information back out.
voice your opinion now!
database class iterator implement magicmethod
|
Community Events
Don't see your event here? Let us know!
|