spl to the rescue - tek 09

55
Standard Tools for Everyday Programming

Upload: elizabeth-smith

Post on 08-May-2015

1.690 views

Category:

Technology


1 download

DESCRIPTION

Given at tek 2009 this is the first version of my SPL to the Rescue talk

TRANSCRIPT

Page 1: SPL to the Rescue - Tek 09

Standard Tools for Everyday Programming

Page 2: SPL to the Rescue - Tek 09

Community Heckling On twitter #tek09 (or #phptek) and #spldg

DG’s for drinking game (you’ll 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 –constructive criticism is welcome ;)

Page 3: SPL to the Rescue - Tek 09

I have a ProblemRecursively iterate through

directories

Find all .jpg files

Check last modified dates

Moved the ones older than two years to new location

Page 4: SPL to the Rescue - Tek 09

How should I do this? Some nasty recursive use of scandir() to get my lists

Or PHP’s dir() function and looping

array_map() with a convoluted callback

I think I’m going to need a lot of code….

Page 5: SPL to the Rescue - Tek 09
Page 6: SPL to the Rescue - Tek 09

SPL to the Rescue! RecursiveDirectoryIterator

RecursiveIteratorIterator

FilterIterator

SplFileInfo

What fun tools we have!

Page 7: SPL to the Rescue - Tek 09
Page 8: SPL to the Rescue - Tek 09

And not the kind you kick out of IRC

Page 9: SPL to the Rescue - Tek 09

What is SPL?tandard HP ibrary

A library of standard interfaces, classes, and functions designed to

solve common programming problems and allow engine

overloading.

Page 10: SPL to the Rescue - Tek 09

That’s nice… in English please.What is SPL?1. Engine overloading hooks via interfaces

ArrayAccess, Countable, SeekableIterator

2. Classes that utilize the interfaces do cool things ArrayObject, RecursiveIterator, DirectoryIterator

3. Standard Class Implementations Exceptions, SplObserver and SplStorage

4. Functions to help with autoloading and objects spl_autoload_register(), spl_classes(), iterator_apply()

Page 11: SPL to the Rescue - Tek 09

But… it’s an extension right? 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 don’t have SPL, whoever built your PHP is an idiot

(or an evil genius – it’s HARD).

Page 12: SPL to the Rescue - Tek 09

Helper functions from SPL to you

Page 13: SPL to the Rescue - Tek 09

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() – what’s on the stack

spl_autoload_extensions() – ext for spl_autoload()

spl_autoload_call() – load something through the stack

Page 14: SPL to the Rescue - Tek 09

Isn’t __autoload good enough? Combining different libraries with different naming

conventions

Dealing with different types of files (templates, classes) in different locations

Changing autoload in use during runtime

Page 15: SPL to the Rescue - Tek 09

Object Helper Functions class_implements()

class_parents()

spl_object_hash()

Why are these not in core?

I DON’T KNOW - GO ASK YOUR DAD!

Page 16: SPL to the Rescue - Tek 09

Nothing but Templates

Page 17: SPL to the Rescue - Tek 09

Exception Classes

LogicException

BadFunctionCallException

BadMethodCall

DomainExceptionInvalidArgumentE

xceptionLengthException

OutofRangeException

Page 18: SPL to the Rescue - Tek 09

Exception Classes

RuntimeException

OutofBoundsException

OverflowException

RangeExceptionUnderflowExcepti

onUnexpectedValueE

xception

Page 19: SPL to the Rescue - Tek 09

So what does SPL offer? A standard set of Exceptions that all inherit from

PHP’s 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.

Page 20: SPL to the Rescue - Tek 09

Foreach is your bestest friend! Foreach an object today!

Page 21: SPL to the Rescue - Tek 09

The iterator drinking game! Every time someone says the word iterator tonight,

take a drink

Start a conversation with me, and you’ll be gone in about five minutes

It’s the SPL drinking game (tonight at cocktail hour)

Page 22: SPL to the Rescue - Tek 09

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?

Page 23: SPL to the Rescue - Tek 09

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

(that’s 5 shots)….

Page 24: SPL to the Rescue - Tek 09

Meet the Iterator Interface

Page 25: SPL to the Rescue - Tek 09

So how is it different?Array $ar= array(); Iterator $it = new Iterator;

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)

Might be rewindable

$it->rewind()

should know if there is a value

$it->valid()

Might have a current value or key

$it->key()

$it->current()

Can move forward

$it->next()

Page 26: SPL to the Rescue - Tek 09

An Iterator for every occasion RecursiveIterator

RecursiveIteratorIterator

OuterIterator

IteratorIterator

FilterIterator

RecursiveFilterIterator

ParentIterator

SeekableIterator

LimitIterator

GlobIterator

• CachingIterator

• RecursiveCachingIterator

• NoRewindIterator

• AppendIterator

• RecursiveIteratorIterator

• InfiniteIterator

• RegexIterator

• RecursiveRegexIterator

• EmptyIterator

• RecursiveTreeIterator

• ArrayIterator

Page 27: SPL to the Rescue - Tek 09

See, the drinking game will be lots of fun….

Page 28: SPL to the Rescue - Tek 09

Innie or an Outie? OuterIterator (interface)

Extends Iterator

Puts a wrapper around an iteratorinside

Has one additional method –getInnerIterator() that should be implemented

Page 29: SPL to the Rescue - Tek 09

Loopety Loop RecursiveIterator

(interface)

Has two additional methods to implement

getChildren should return the sub-iteratorfor the current element – and it must return an object that implements recursiveIterator

hasChildren

Page 30: SPL to the Rescue - Tek 09

Jumping ahead? SeekableIterator

(interface)

Additional method –seek(string $position)

Lets you jump to a specific spot to start iterating

Page 31: SPL to the Rescue - Tek 09

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 can’t extend multiple classes

Choose Wisely

Page 32: SPL to the Rescue - Tek 09

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

Page 33: SPL to the Rescue - Tek 09

IteratorIterator Regular Class

Iterates an iterator – No I am not kidding

IteratorIterator OuterIterator Iterator Traversable

Page 34: SPL to the Rescue - Tek 09

ArrayIterator Regular Class

Iterates an array – OR the public properties of an object! (neat trick – dirty trick)

ArrayIterator SeekableIterator Iterator TraversableArrayAccess and Countable too!

Page 35: SPL to the Rescue - Tek 09

RecursiveIteratorIterator Regular Class

Like IteratorIterator only recursive to boot – still not kidding - but I don’t say it as cool as Marcus

RecursiveIteratorIterator OuterIterator Iterator Traversable

Page 36: SPL to the Rescue - Tek 09

ParentIterator Regular Class

Filter out stuff without children

ParentIterator OuterIterator Iterator Traversable

Page 37: SPL to the Rescue - Tek 09

LimitIterator Regular Class

Like mysql’s limit – pick your range and offset and foreach away!

LimitIterator OuterIterator Iterator Traversable

Page 38: SPL to the Rescue - Tek 09

CachingIterator Regular Class

Manages another iterator by checking whether it has more elements each time using a hasNext() method

CachingIterator OuterIterator Iterator Traversable

Page 39: SPL to the Rescue - Tek 09

RecursiveCachingIterator Regular Class

Just like caching iterator only – believe it or not –recursive!

RecursiveCachingIterator CachingIterator OuterIterator Iterator Traversable

Page 40: SPL to the Rescue - Tek 09

DirectoryIterator Regular Class

Makes going through directories a snap

isDot, isFile – I love you

DirectoryIteratorSplFileInfo(extends)

Iterator Traversable

Page 41: SPL to the Rescue - Tek 09

RecursiveDirectoryIterator Regular Class

Like, do it again… and again… and again… and…

DirectoryIterator(extends)

RecursiveIterator Iterator Traversable

Page 42: SPL to the Rescue - Tek 09

Iterator Helper Functions iterator_apply() – like array_walk for your iterator

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

doesn’t work alone

Page 43: SPL to the Rescue - Tek 09

Is it an array? An object? Why… it’s both!

Page 44: SPL to the Rescue - Tek 09

ArrayAccess Interface

Page 45: SPL to the Rescue - Tek 09

ArrayObject A class, NOT an interface

It’s like arrayaccess on RedBull

The manual LIES – for a full list of everything you can do with arrayobject, see http://www.php.net/~helly/php/ext/spl/

Highlights exchangeArray

getArrayCopy (get your internally stored array)

Sorting methods ksort et al

Page 46: SPL to the Rescue - Tek 09

Countable Interface you can

implement with any class (not iteratorspecific, but used a lot for it)

Implement the count method and you can use the count() PHP function on any object

Page 48: SPL to the Rescue - Tek 09

SPLFixedArray A fixed length, int key only array

Why? It’s FAST (very fast) because it stores data differently “under the hood” in C

Regular class, don’t need to extend and fill in any methods

5.3+

Page 50: SPL to the Rescue - Tek 09

SplFileInfo fancy class for a file

all the file system functions in compact object form

getMTime == filemtime

openFile == fopen

Page 51: SPL to the Rescue - Tek 09

Beyond arrays to the wild wild west

Page 52: SPL to the Rescue - Tek 09

New and Shiny

SplDoublyLinkedList

SplStack SplQueue

Page 53: SPL to the Rescue - Tek 09

More Data Structures

SplHeap

SplMaxHeap SplMinHeap

SplPriorityQueue