ZCE Array: Standard PHP Library (SPL)
The Standard PHP Library (SPL) attempts to solve standard problems and implements efficient data access interfaces and classes.
I have little understanding of SPL! So much research gone in this section
When asking on forums, I receaved two links to the PHP Manual’s here they are:
http://us.php.net/spl
http://www.php.net/~helly/
These obviosly little help as its just listing of functions and complex definitions that you wont understand unless you allready know!
Sitepoint.com, (I am a big fan off) fortunately have a article on SPL in general:
http://www.sitepoint.com/article/php5-standard-library/
SPL extension makes it possible to “overload” basic PHP syntax and make objects look like normal PHP arrays.
Today the SPL extension addresses a single subset of problems: Iterators. What makes the SPL Iterator implementation interesting is not only that it defines a standard for everyone to use in PHP5, but also that it “overloads” certain parts of PHP syntax such as the foreach construct and basic array syntax, making it easier to work with objects of your classes.
SPL is new and baddly documented. It is unfinished and many changes are expected. If you have any more learning resorces for SPL then post them bellow! Help Others and I.
Introduction to SPL
As I suggested sitepoint members pointed me to [ THIS ], a article that has been removed but with archive.org I have restored. Read Inroduction To Standard PHP Libary or continue reading for my overview.
What Is SPL
SPL is used to “traverse aggregate structures” (anything you want to loop over). It is used with Arrays, databse restults, xml treels, director listings or any other list.
Put Basicly, its a way of outputing/useing data in many forms of lists. SPL allows you do do this with OOP meaning a lot less code and complexity, it also allows errors to the “thrown” and handled how you wish.
Not only does SPL lets you go forwards/backwards in lists, it adds additional perimeters such as isFile() checks if the element from the list is a file, alternatively theirs isDir() this checks that the element is a directory.
List of SPL Classes:
Lets presume we dont need to know all of these to become a ZCE, but we should be aware of them.
1. Iterators
RecursiveIterator
OuterIterator
RecursiveIteratorIterator
FilterIterator
ParentIterator
SeekableIterator
LimitIterator
CachingIterator
RecursiveCachingIterator
IteratorIterator
NoRewindIterator
EmptyIterator
InfiniteIterator
AppendIterator
RegexIterator
RecursiveRegexIterator
2. SPL Directories and Files Handling
SplFileInfo
DirectoryIterator
RecursiveDirectoryIterator
SplFileObject
3. SPL XML Handling
SimpleXMLIterator
4 SPL Array Overloading
ArrayObject
ArrayIterator
RecursiveArrayIterator
As the above suggest an ArrayObject creates an ArrayIterator when it comes to iteration (e.g. ArrayObject instance used inside foreach).
5. Counting
Countable allows to hook into the standard array function count().
6. Exceptions
SPL provides a set of standard Exception classes each meant to indicate a certain problem type.
LogicException
BadFunctionCallException
BadMethodCallException
DomainException
InvalidArgumentException
LengthException
OutOfRangeException
RuntimeException
OutOfBoundsException
OverflowException
RangeException
UnderflowException
7. Observer
SPL suggests a standard way of implementing the observer pattern.
SplObserver
SplSubject
SplObjectStorage
What are Iterators
An iterator is an object (class) that goes trough every element in a structure (e.g. array, directory listing or database results). Their are different types of iterators for the different types of data such as array iterators and directory iterators.
DirectoryIterators
SPL is far simpler than I first thought, it is a improved method to make use of lists of data. A section of the SPL is dedicated to directorys and files.
In the past if we wanted to list files and folders in a directory it would be complex, highly unlikly you can code that off top of your head:
$hdl = opendir('./'); while ($dirEntry = readdir($hdl)) { if (substr($dirEntry, 0, 1) != '.') { if(!is_file($dirEntry)) { continue; } $listing[] = $dirEntry; } } closedir($hdl); foreach($listing as $my_file) { echo $my_file.'<br />'; }
Now with PHP5’s SPL it becomes a lot more simpler with the new SPL commands,
<?php try{ /*** class create new DirectoryIterator Object ***/ foreach ( new DirectoryIterator('./') as $Item ) { echo $Item.'<br />'; } } /*** if an exception is thrown, catch it here ***/ catch(Exception $e){ echo 'No files Found!<br />'; } ?>
The code above will produce a simple list of all files and directories within a current working directory that is passed directly to the __construct(). This is all good, but we can do so much more.
SPL in Depth
SPL goes on and on.. So should you to read:
http://zend.is-hacked.com/spl/
1 Comment »
RSS feed for comments on this post. TrackBack URL












You might give this a look: http://devzone.zend.com/article/2565-the-standard-php-library-spl