Tag Archives: gist


SPL iterators with closure context switching

Some time ago I redi­sco­ve­red PHP SPL library. After reading Joshua Thijssen’s Maste­ring the SPL Library I found out that some­ti­mes I was reinven­ting the wheel in my code, sorry (employ­ers and custo­mers). Itera­tors are one of the SPL library compo­nents. To add more spice to code I’ll show some kind of Java­Script magic using closures.

Simplest and best itera­tor definition:

Itera­tors are PHP’s way of dealing with abstrac­ting away the traver­sal logic from the busi­ness logic. This, in one sentence, sums up what an itera­tor is. [J. Thijs­sen : Maste­ring…, p. 68]

My goal for today is to show you how to use Itera­to­rAg­gre­gate with Call­back­Fil­te­rI­te­ra­tors to filter out files not meeting our requ­ire­ments.

Feel free to use this class in your own projects.


Using streams in multi process environment

Today is the last instal­l­ment about PHP stre­ams. This time we will borrow some images from GitHub octo­dex using remote stre­ams and stream copy­ing. To add some spice, we’re going to use PHP in paral­lel proces­sing. If you thought PHP is single process srcrip­ting langu­age, I will ruin your world, sorry ;-)

Of course in real world projects there is always a trade off between time and reso­ur­ces. Forking process is CPU expen­sive work for server. We’re assu­ming we have plenty of CPU cycles and RAM, but short on time. Take a look on code below:

It took 5 secs to fetch 127 images. The same thing run in single fore­ach loop takes about 2 minutes…


Writing custom stream filters

Perhaps you used once or twice buil­tin zlib.* or mcrypt.* stream filters. Have you tried making your own stream filter? Maybe you didn’t need to. There are ways to accom­plish busi­ness needs without making hands dirty by not well docu­men­ted stuff like user filters. I need to know that topic like I said before, because of certification :-)

We’re going to make stream filter capa­ble of compu­ting hash check­sum from stream contents on the fly. Rich docu­men­ted code below: