Beruflich Dokumente
Kultur Dokumente
Community Heckling
On twitter #tek09 (or #phptek) and #spldg DGs for drinking game (youll see why later) IRC is open, I can see backlog constructive criticism
is good
Comment on http://joind.in/talk/view/186 No comments on hair, clothes, or my fat belly
I have a Problem
Recursively iterate through directories
Find all .jpg files Check last modified dates Moved the ones older than two
What is SPL?
tandard HP ibrary A library of standard interfaces, classes, and functions designed to solve common programming problems and allow engine overloading.
2.
3.
SPL is an extension SPL is a core extension SPL cannot be built shared SPL should not be turned off SPL is present in PHP since 5.0 (almost 5 years ago) As of 5.3, SPL cannot be turned off without altering source
If you dont have SPL, whoever built your PHP is an idiot (or an evil genius its HARD).
Autoload Magic
spl_autoload() default autoload implementation spl_autoload_register() add an autoload to the stack spl_autoload_unregister() remove an autoloader spl_autoload_functions() whats on the stack
stack
conventions Dealing with different types of files (templates, classes) in different locations Changing autoload in use during runtime
Why are these not in core? I DONT KNOW - GO ASK YOUR DAD!
Exception Classes
LogicException
BadFunctionCallE xception
DomainException
InvalidArgumentE xception
LengthException
OutofRangeExcept ion
BadMethodCall
Exception Classes
RuntimeException
OutofBoundsExce ption
OverflowExceptio n
RangeException
UnderflowExcepti on
UnexpectedValueE xception
PHPs Exception base class A standard way to set up exceptions by what kind they are Do I recommend it? Depends on how exceptions are used in your application.
take a drink Start a conversation with me, and youll be gone in about five minutes Its the SPL drinking game (tonight at cocktail hour)
Iterators
What the heck is an iterator? A design pattern that is a generic solution to the problem of iterating over data in a consistent manner. Access the elements of an aggregate object sequentially without exposing its underlying representation. Why do I care? Ever need to go over a list of items returned from a database (well, duh) Or need to go over a list of items returned from a webservice? Ever used foreach?
Foreach it baby!
Foreach is your friend iterators give your code consistent usage and you can add more functionality
What else can you do with iterators? Extend Iterators to do what you need Chain Iterators: iteratoriteratoriteratoriterator
(thats 5 shots).
So how is it different?
Array $ar= array();
can be rewound reset($ar) is valid unless the key is NULL !is_null(key($ar)) Has a current value current($ar) Has keys key($ar) can move forward next($ar)
key
$it->key() $it->current()
ParentIterator
SeekableIterator LimitIterator GlobIterator
RegexIterator
RecursiveRegexIterator EmptyIterator RecursiveTreeIterator ArrayIterator
Innie or an Outie?
OuterIterator (interface) Extends Iterator Puts a wrapper around an iterator inside Has one additional method getInnerIterator() that should be implemented
Loopety Loop
RecursiveIterator
(interface)
Has two additional
methods to implement getChildren should return the sub-iterator for the current element and it must return an object that implements recursiveIterator hasChildren
Jumping ahead?
SeekableIterator
(interface)
Additional method
Now on to classes
Classes implement interfaces plus provide additional
functionality Interfaces need you to fill in all the the required methods You can implement multiple interfaces You cant extend multiple classes Choose Wisely
FilterIterator
Abstract Class Has one method that must be implemented accept
which should return true or false File filtering example at the beginning used this Highly useful for many types of iteration
FilterIterator
OuterIterator
Iterator
Traversable
IteratorIterator
Regular Class Iterates an iterator No I am not kidding
IteratorIterator
OuterIterator
Iterator
Traversable
ArrayIterator
Regular Class Iterates an array OR the public properties of an
ArrayIterator
SeekableIterator
Iterator
Traversable
RecursiveIteratorIterator
Regular Class Like IteratorIterator only recursive to boot still not
RecursiveIteratorIterator
OuterIterator
Iterator
Traversable
ParentIterator
Regular Class Filter out stuff without children
ParentIterator
OuterIterator
Iterator
Traversable
LimitIterator
Regular Class Like mysqls limit pick your range and offset and
foreach away!
LimitIterator
OuterIterator
Iterator
Traversable
CachingIterator
Regular Class Manages another iterator by checking whether it has
CachingIterator
OuterIterator
Iterator
Traversable
RecursiveCachingIterator
Regular Class Just like caching iterator only believe it or not
recursive!
RecursiveCachingIterator
CachingIterator
OuterIterator
Iterator
Traversable
DirectoryIterator
Regular Class Makes going through directories a snap isDot, isFile I love you
DirectoryIterator
SplFileInfo (extends)
Iterator
Traversable
RecursiveDirectoryIterator
Regular Class Like, do it again and again and again and
DirectoryIterator (extends)
RecursiveIterator
Iterator
Traversable
implementing objects iterator_count() count the items in your iterator (not quite the same as implementing countable::count) iterator_to_array() copy all the stuff from your iterator into a regular PHP array Even Superman doesnt work alone
ArrayAccess Interface
ArrayObject
A class, NOT an interface Its like arrayaccess on RedBull The manual LIES for a full list of everything you can
Countable
Interface you can
implement with any class (not iterator specific, but used a lot for it) Implement the count method and you can use the count() PHP function on any object
SplObjectStorage
This does not do what you think it does Use objects as array keys, uniquely, with no collision
issues (you might get them from spl_object_hash) Remember you need the object to get the data back out, unless youre simply iterating over the contents Regular class, no need to extend and fill in methods
http://www.colder.ch/news/01-08-
2009/34/splobjectstorage-for-a-fa.html
SPLFixedArray
A fixed length, int key only array Why? Its FAST (very fast) because it stores data
differently under the hood in C Regular class, dont need to extend and fill in any methods 5.3+
SplFileInfo
fancy class for a file all the file system functions in compact object form getMTime == filemtime openFile == fopen
SplStack
SplQueue
SplMaxHeap
SplMinHeap
Resources
http://php.net/spl - spl docs http://php.net/~helly/php/ext/spl - Doxygen docs http://blueparabola.com/blog/spl-deserves-some-