Another area of
PHP 5 that is used by the Zend Framework is the Standard PHP Library, known as the SPL.
17.2 The SPL
The Standard PHP Library, aka SPL, is a library of useful interfaces designed to make data access easier. The
SPL provides the following categories of interfaces:
Iterators ??“ for looping over elements in a collection, files, directories or XML.
Array Access ??“ for making objects act like arrays
Counting ??“ to allow objects to work with count()
Observer ??“ an implementation of the Observer software design pattern
17.2.1 Iterators
Iterators are objects that loop (or traverse) a structure such as an array, database result set or a directory listing.
You need an iterator for each type of structure that you wish to iterate over and the most common (arrays,
directories) are built into PHP and the SPL directly. A common use-case is to read a directory and this is done
using the SPL??™s DirectoryIterator object as shown in listing 17.10.
Listing 17.10 Iteration over a directory listing using DirectoryIterator
$dirList = new DirectoryIterator('/'); 1
foreach ($dirList as $item) { A
echo $item . "\n";
Licensed to Menshu You
Zend Framework in Action (Ch01) Manning Publications Co. 56
}
1 Create iterator
A Use iterator in foreach() to loop
As you can see, using an iterator is as easy as using a foreach() loop and that??™s the whole point! Iterators
make accessing collections of data very very easy and they come into their own when used with custom
classes.
Pages:
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379