spl in the wild

50
SPL IN THE WILD The Good, the Bad and the Ugly

Upload: elizabeth-smith

Post on 08-May-2015

2.328 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Spl in the wild

SPL IN THE WILD The Good, the Bad and the Ugly

Page 2: Spl in the wild

If you don’t know SPL

basics, this talk might go

fast for you.

Please rate me -

Feedback is good!

Page 3: Spl in the wild

Why SPL?

What SPL?

How SPL? A library of standard interfaces,

classes, and functions designed to

solve common programming

problems and allow engine

overloading

Page 4: Spl in the wild

BUT ISN’T SPL AN EXTENSION?

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

If you don’t have SPL, whoever built your PHP is an idiot.

Page 5: Spl in the wild

AUTOLOAD

Don’t let friends use __autoload

Page 6: Spl in the wild

AUTOLOAD STACK ERR QUEUE

spl_autoload_register()

spl_autoload_unregister()

spl_autoload_call()

spl_autoload_functions()

https://gist.github.com/221634

Page 7: Spl in the wild

BEWARE THE WEIRDNESS

spl_autoload()

lowercase

relative paths

namespace aware

spl_autoload_extensions()

include the .

no spaces in between

comma separated string

Page 9: Spl in the wild

EXCEPTIONS What went wrong where

Page 10: Spl in the wild

EXCEPTION CLASSES

LogicException

BadFunctionCall

Exception

BadMethodCall

Exception

InvalidArgument

Exception

Domain

Exception

Length

Exception

OutofRange

Exception

Page 11: Spl in the wild

EXCEPTION CLASSES

RuntimeException

OutofBounds

Exception

Overflow

Exception

Range

Exception

UnderflowException

UnexpectedValue

Exception

Page 13: Spl in the wild

FILES Yes PHP has a file object

Page 14: Spl in the wild

SPLFILEINFO

Metadata about a file

Returned by directory iterators

Can set your own superclass for splfileinfo/splfileobject

In the Wild

phpcmc

https://github.com/fqqdk/phpcmc/blob/master/tests/p

hpcmc/micro/PhpLinterTest.php

Page 15: Spl in the wild

SPLFILEOBJECT

Open file pointer

Extends splfileinfo

In the Wild

Utils Plugin for CakePHP

https://github.com/CakeDC/utils/blob/master/Mo

del/Behavior/CsvImportBehavior.php

Page 16: Spl in the wild

ARRAYS AS OBJECTS

Magic objects oh my

Page 17: Spl in the wild

SPECIFICALLY ARRAYOBJECT

Implements ArrayAccess (with references)

Implements Countable

Implements IteratorAggregate

Implements Serializable (since 5.3.0)

And other methods that arrays can use (but not all)

In the Wild

Rapide Framework

https://github.com/Hanse/rapide-

framework/blob/master/lib/Rapide/Utility/ArrayObject.php

Page 18: Spl in the wild

INTERFACES

Magic and organization

Page 21: Spl in the wild

SPL - COUNTABLE

Interface meaning “you can count me”

Can be put on any class

Makes count() magical

Note this is NOT the same as iterator_count()

https://github.com/ysbaddaden/php5-

redis/blob/master/lib/Redis/Servers.php

Page 22: Spl in the wild

ITERATOR INTERFACES

Outer Iterator

Inner Iterator

Seekable Iterator

For examples, we’ll look at the iterator classes

Page 23: Spl in the wild

SPLSUBJECT SPLOBSERVER

Are you implementing the observer pattern in your code?

Do you intend to have other people use your code/library in some way?

Are you implementing something LIKE the observer pattern?

In the Wild

EmailLabs_Sync (SplSubject)

https://github.com/sankovicmarko/EmailLabs_Sync/blob/master/library/EmailLabs/Logger.php

Frapi

https://github.com/frapi/frapi/blob/master/src/frapi/library/PEAR/HTTP/Request2/Observer/Log.php

Page 24: Spl in the wild

ITERATORS

Take a Drink …

Page 25: Spl in the wild

(RECURSIVE)FILTERITERATOR

Abstract Class

Has one method that must be implemented – accept – which should

return true or false

Highly useful for many types of iteration

https://github.com/nishimura/laiz/blob/master/laiz/builder/AutoIncludeFil

ter.php

https://github.com/ralphschindler/PHPTools/blob/master/library/PHPToo

ls/Namespacer/RecursiveFilterIterator.php

FilterIterator OuterIterator Iterator Traversable

Page 27: Spl in the wild

(RECURSIVE)ARRAYITERATOR

Regular Class

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

dirty trick)

https://github.com/diggin/Diggin_Service_Wedata/blob/master/src/Diggin

/Service/Wedata/Items.php

https://github.com/Respect/Validation/blob/master/library/Respect/Valida

tion/ExceptionIterator.php

ArrayIterator

SeekableIterator

Iterator Traversable ArrayAccess

and Countable too!

Page 28: Spl in the wild

APPENDITERATOR

Keep stacking more iterators on the end with append

https://github.com/WebToad/FashionPolice/blob/master/libs/Nette/Utils/

Finder.php

ParentIterator OuterIterator Iterator Traversable

Page 29: Spl in the wild

LIMITITERATOR

Regular Class

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

https://github.com/jasir/ComponentTreePanel/blob/master/ComponentT

reePanel.php

LimitIterator OuterIterator Iterator Traversable

Page 30: Spl in the wild

FILESYSTEMITERATOR

Extends directory iterator

Lets you choose how to get data (just string names possible)

https://github.com/KnpLabs/Gaufrette/blob/master/tests/Gaufrette/Adap

ter/LocalTest.php

FileSystemIterator DirectoryIterator Iterator Traversable

Page 33: Spl in the wild

RECURSIVETREEITERATOR

Regular Class

Can create an ascii graphic tree (seriously…)

https://github.com/Respect/Validation/blob/master/library/Respect/Valida

tion/Exceptions/AbstractNestedException.php

RecursiveTreeIterator

RecursiveIteratorIterator

OuterIterator Iterator Traversable

Page 35: Spl in the wild

EMPTY AND INFINITE ITERATORS

In the Wild – EmptyIterator

https://github.com/evilgeny/bob/blob/master/romir/projects/libraries/Bar

code/Mapper.class.php

Infinite? Really only useful for testing

InfiniteIterator IteratorIterator Iterator Traversable

EmptyIterator Iterator Traversable

Page 36: Spl in the wild

MORE EXIST

ParentIterator

NoRewindIterator

MultipleIterator

GlobIterator

CallbackFilterIterator

RecursiveCallbackFilterIterator

Page 37: Spl in the wild

DATASTRUCTURES

New ways of managing data

Page 38: Spl in the wild

DOUBLYLINKEDLISTS – CS LESSON

ordered collection of values

linked to each element before it

linked to each element after it

“doubly linked”

PHP datastructure – a php object with a doublylinkedlist stored inside it

Page 39: Spl in the wild

SPLDOUBLYLINKEDLIST

Don’t use this

Yes, that’s a terrible thing to say – but this is really nothing more then a

“base class” with little to recommend on its own

Has a doublylinkedlist from C underneath instead of a hashtable – if you

know what that means you may find a real use for this (I have not)

https://github.com/osebboy/Notification/blob/master/src/Notification/Di

spatcher.php

Page 40: Spl in the wild

SPLSTACK

Data is in LIFO

Anything you need to iterate a lot

Even cooler? Turn on the mode that will autodelete each item as you

process it

Any Queue you need to push stuff onto and deal with in LIFO order

https://github.com/rsesek/phalanx/blob/master/tasks/task_pump.php

Page 41: Spl in the wild

SPLQUEUE

Data is in FIFO

Anything you need to iterate a lot

Even cooler? Turn on the mode that will autodelete each item as you process it

Any Queue you need to push stuff onto and deal with in LIFO order

https://github.com/matthewshafer/fauxThread/blob/master/src/fauxThreadPool.php

Page 42: Spl in the wild

HEAP – QUICK CS LESSON

comparison function used to compare the new element to other

elements

element is placed according to functions return value

underlying algorithm does it with minimal comparisons

PHP datastructure – a php object with a heap stored inside it

Page 44: Spl in the wild

SPLMINHEAP, SPLMAXHEAP

These are concrete heap implementations, designed to grab the lowest

possible value out, or the highest possible

https://github.com/tobyS/php-

snippets/blob/master/datastructures/bottom_k.php

https://github.com/stormbreakerbg/A---A-Star--pathfinding-class-in-

PHP/blob/master/AStarWithHeap.php

Page 45: Spl in the wild

SPLPRIORITYQUEUE

Uses heap internally

Is non-deterministic when identical priorities are used

https://github.com/ss23/DeBot/blob/master/core/SplFIFOPriorityQueue.

php

Page 46: Spl in the wild

SPLFIXEDARRAY

You have a large amount of data, you know the final size, you need to

stick it into an array

You’re not going to expand it past the final size

This is not a small amount of data

You might need it in non-sequential order but can handle having only

integers for keys

https://github.com/cboden/gClient/blob/master/lib/gClient/Calendar/Cal

endar.php

Page 47: Spl in the wild

SPLOBJECTSTORAGE

This can be used two ways

Objects can be keys in an array (with two values)

As a Set (with one value)

https://github.com/greggles/epm_project_management/blob/master/QueryPath/CssEventHandler.php

Page 48: Spl in the wild

WHAT DO YOU WANT TO SEE IN SPL?

More standard interface?

More datastructures?

trees?

graphs?

More iterators? really? more?

Page 49: Spl in the wild

GET INVOLVED

http://edit.php.net

Blog posts

Articles

Use SPL in the wild

Page 50: Spl in the wild

CONTACT ME

http://emsmith.net

https://joind.in/6228

[email protected]

IRC – freenode – auroraeosrose

#coapp and others