With this presentation I hope to show that using SPL doesn't require a PHD and that it really benefits your application design, maintainability and implements best practices to solve common development problems.
1. SPL, not a bridge too far
Dutch PHP Conference 2009 - Amsterdam
2. Who am I ?
Michelangelo van Dam
Independent Enterprise PHP consultant
@dragonbe
http://dragonbe.com
2
3. Who are you ?
You know OOP ?
You know php.net ?
You know arrays ?
3
4. What is SPL ?
Standard PHP Library
interfaces, classes and methods
solve common development problems
As of 5.3 SPL cannot be turned off from the source !
4
5. Why use SPL ?
SPL provides a huge toolkit that assists you to easily
iterate over a diversity of data structures in a
standardized way
5
6. What does it provide ?
• ArrayObject - approach arrays as objects
• Iterators - various iterators
• Interfaces - Countable Interface
• Exceptions - exceptions to streamline errorhandling
• SPL Functions - extra functions and autoloader func
• SplFileInfo - tools for filesystem access
• Datastructures - structuring data sequences
6
7. ArrayObject
• provides an interface
- treat arrays as objects
- elements are iteratable
7
15. ArrayIterator Example Case
• Array with 3 elements
- monkey => bananas,
- rabbit => carrots,
- ogre => onions
• display total elements
• display key/value pairs while looping
• display key/value pair for 2nd element
15
25. <?php
Exceptions Example
//file: spl_exception01.php
class MyClass
{
public function giveANumberFromOneToTen($number)
{
if($number < 1 || $number > 10) {
throw new OutOfBoundsException('Number should be between 1 and 10');
}
echo $number . PHP_EOL;
}
}
$my = new MyClass();
try {
$my->giveANumberFromOneToTen(5);
$my->giveANumberFromOneToTen(20);
} catch (OutOfBoundsException $e) {
echo $e->getMessage() . PHP_EOL;
}
Output:
$ /usr/bin/php ./spl_exception01.php
5
Number should be between 1 and 10
25
26. SPL Functions
• class_implements
Return the interfaces which are implemented by the given class
• class_parents
Return the parent classes of the given class
• iterator_apply
Apply a user function to every element of an iterator
• iterator_count
Count the elements in an iterator
• iterator_to_array
Copy the iterator into an array
• spl_autoload_call
Try all registered __autoload() function to load requested class
26
27. SPL Functions (2)
• spl_autoload_functions
Return all registered __autoload() functions
• spl_autoload_register
Register given function as __autoload() implementation
• spl_autoload_unregister
Unregister given function as __autoload() implementation
• spl_autoload
Default implementation for __autoload()
• spl_classes
Return available SPL classes
• spl_object_hash
Return hash id for given object
27
28. <?php
SplFunctions Example
interface foo {}
interface bar {}
class baz implements foo, bar {}
class example extends baz {}
var_dump(class_implements(new baz));
var_dump(class_implements(new example));
28
32. <?php
SplFileInfo Example
// use the current file to get information from
$file = new SplFileInfo(dirname(__FILE__));
var_dump($file->isFile());
var_dump($file->getMTime());
var_dump($file->getSize());
var_dump($file->getFileInfo());
var_dump($file->getOwner());
//output
bool(false)
int(1244760945)
int(408)
object(SplFileInfo)#2 (0) {
}
int(501)
32
33. Data Structures
• Available in PHP 5.3.x
- SplDoublyLinkedList
✓ SplStack
✓ SplQueue
✓ SplHeap
✓ SplMaxHeap
✓ SplMinHeap
✓ SplPriorityQueue
33
38. Documentation problem
• php.net/spl needs more documentation !
• you can help on that part:
- see http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows
- you can set up phpdoc on each platform !
- Efnet: #php.doc channel
- http://doc.php.net - phpdoc@php.net
38
39. More about SPL...
• main SPL documentation:
http://php.net/spl
• PHPro Tutorials on SPL:
http://www.phpro.org/tutorials/Introduction-to-SPL.html
• Lorenzo Alberton’s blog:
http://www.alberton.info/php_5.3_spl_data_structures.html
• http://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-
a-fa.html
39
40. License
This presentation is released under the Creative Commons
Attribution-Share Alike 3.0 Unported License
You are free:
- to share : to copy, distribute and transmit the work
- to remix : to adapt the work
Under the following conditions:
- attribution :You must attribute the work in the manner specified by the author or licensor
- share alike : If you alter, transform, or build upon this work, you may distribute the resulting work
only under the same, similar or a compatible license
See: http://creativecommons.org/licenses/by-sa/3.0/
40