Standard PHP Library

download Standard PHP Library

of 55

Transcript of Standard PHP Library

  • 7/28/2019 Standard PHP Library

    1/55

    Standard Tools for Everyday Programming

  • 7/28/2019 Standard PHP Library

    2/55

    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 criticismis good

    Comment on http://joind.in/talk/view/186

    No comments on hair, clothes, or my fat bellyconstructive criticism is welcome ;)

    http://joind.in/talk/view/186http://joind.in/talk/view/186
  • 7/28/2019 Standard PHP Library

    3/55

    I have a ProblemRecursively iterate through

    directories

    Find all .jpg files

    Check last modified dates

    Moved the ones older than twoyears to new location

  • 7/28/2019 Standard PHP Library

    4/55

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

    Or PHPs dir() function and looping

    array_map() with a convoluted callback

    I think Im going to need a lot of code.

  • 7/28/2019 Standard PHP Library

    5/55

  • 7/28/2019 Standard PHP Library

    6/55

    SPL to the Rescue! RecursiveDirectoryIterator

    RecursiveIteratorIterator

    FilterIterator SplFileInfo

    What fun tools we have!

  • 7/28/2019 Standard PHP Library

    7/55

  • 7/28/2019 Standard PHP Library

    8/55

    And not the kind you kick out of IRC

  • 7/28/2019 Standard PHP Library

    9/55

    What is SPL?tandard HP ibrary

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

    solve common programmingproblems and allow engine

    overloading.

  • 7/28/2019 Standard PHP Library

    10/55

    Thats 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()

  • 7/28/2019 Standard PHP Library

    11/55

    But its 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 dont have SPL, whoever built your PHP is an idiot

    (or an evil genius its HARD).

  • 7/28/2019 Standard PHP Library

    12/55

    Helper functions from SPL to you

  • 7/28/2019 Standard PHP Library

    13/55

    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

    spl_autoload_extensions() ext for spl_autoload()

    spl_autoload_call() load something through the

    stack

  • 7/28/2019 Standard PHP Library

    14/55

    Isnt __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

  • 7/28/2019 Standard PHP Library

    15/55

    Object Helper Functions class_implements()

    class_parents()

    spl_object_hash()

    Why are these not in core?

    I DONT KNOW- GO ASK YOUR DAD!

  • 7/28/2019 Standard PHP Library

    16/55

    Nothing but Templates

  • 7/28/2019 Standard PHP Library

    17/55

    Exception ClassesLogicException

    BadFunctionCallException

    BadMethodCall

    DomainExceptionInvalidArgumentE

    xceptionLengthException

    OutofRangeException

  • 7/28/2019 Standard PHP Library

    18/55

    Exception Classes

    RuntimeException

    OutofBoundsException

    OverflowException

    RangeExceptionUnderflowExcepti

    onUnexpectedValueE

    xception

  • 7/28/2019 Standard PHP Library

    19/55

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

    PHPs Exception base class

    A standard way to set up exceptions by what kind theyare

    Do I recommend it? Depends on how exceptions areused in your application.

  • 7/28/2019 Standard PHP Library

    20/55

    Foreach is your bestest friend! Foreach an object today!

  • 7/28/2019 Standard PHP Library

    21/55

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

    take a drink

    Start a conversation with me, and youll be gone inabout five minutes

    Its the SPL drinking game (tonight at cocktail hour)

  • 7/28/2019 Standard PHP Library

    22/55

    IteratorsWhat the heck is an iterator?

    A design pattern that is a generic solution to theproblem of iterating over data in a consistent manner.

    Access the elements of an aggregate object sequentiallywithout 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?

  • 7/28/2019 Standard PHP Library

    23/55

    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).

  • 7/28/2019 Standard PHP Library

    24/55

    Meet the Iterator Interface

  • 7/28/2019 Standard PHP Library

    25/55

    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 orkey

    $it->key() $it->current()

    Can move forward $it->next()

  • 7/28/2019 Standard PHP Library

    26/55

    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

  • 7/28/2019 Standard PHP Library

    27/55

    See, the drinking game will be lots of fun.

  • 7/28/2019 Standard PHP Library

    28/55

    Innie or an Outie? OuterIterator (interface)

    Extends Iterator

    Puts a wrapper around an iteratorinside

    Has one additional method getInnerIterator() that should beimplemented

  • 7/28/2019 Standard PHP Library

    29/55

    Loopety Loop RecursiveIterator

    (interface)

    Has two additional

    methods to implement getChildren should

    return the sub-iteratorfor the current element

    and it must return anobject that implementsrecursiveIterator

    hasChildren

  • 7/28/2019 Standard PHP Library

    30/55

    Jumping ahead? SeekableIterator

    (interface)

    Additional method seek(string $position)

    Lets you jump to aspecific spot to startiterating

  • 7/28/2019 Standard PHP Library

    31/55

    Now on to classes Classes implement interfaces plus provide additional

    functionality

    Interfaces need you to fill in all the the requiredmethods

    You can implement multiple interfaces

    You cant extend multiple classes

    Choose Wisely

  • 7/28/2019 Standard PHP Library

    32/55

    FilterIteratorAbstract 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

  • 7/28/2019 Standard PHP Library

    33/55

    IteratorIterator Regular Class

    Iterates an iterator No I am not kidding

    IteratorIterator OuterIterator Iterator Traversable

  • 7/28/2019 Standard PHP Library

    34/55

    ArrayIterator Regular Class

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

    ArrayIterator SeekableIterator Iterator TraversableArrayAccess and

    Countable too!

  • 7/28/2019 Standard PHP Library

    35/55

    RecursiveIteratorIterator Regular Class

    Like IteratorIterator only recursive to boot still notkidding - but I dont say it as cool as Marcus

    RecursiveIteratorIterator OuterIterator Iterator Traversable

  • 7/28/2019 Standard PHP Library

    36/55

    ParentIterator Regular Class

    Filter out stuff without children

    ParentIterator OuterIterator Iterator Traversable

  • 7/28/2019 Standard PHP Library

    37/55

    LimitIterator Regular Class

    Like mysqls limit pick your range and offset andforeach away!

    LimitIterator OuterIterator Iterator Traversable

  • 7/28/2019 Standard PHP Library

    38/55

    CachingIterator Regular Class

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

    CachingIterator OuterIterator Iterator Traversable

  • 7/28/2019 Standard PHP Library

    39/55

    RecursiveCachingIterator Regular Class

    Just like caching iterator only believe it or not recursive!

    RecursiveCachingIterator CachingIterator OuterIterator Iterator Traversable

  • 7/28/2019 Standard PHP Library

    40/55

    DirectoryIterator Regular Class

    Makes going through directories a snap

    isDot, isFile I love you

    DirectoryIteratorSplFileInfo(extends)

    Iterator Traversable

  • 7/28/2019 Standard PHP Library

    41/55

    RecursiveDirectoryIterator Regular Class

    Like, do it again and again and again and

    DirectoryIterator(extends)

    RecursiveIterator Iterator Traversable

  • 7/28/2019 Standard PHP Library

    42/55

    Iterator Helper Functions iterator_apply() like array_walk for your iterator

    implementing objects

    iterator_count() count the items in your iterator (notquite the same as implementing countable::count)

    iterator_to_array() copy all the stuff from youriterator into a regular PHP array

    Even Superman

    doesnt work alone

  • 7/28/2019 Standard PHP Library

    43/55

    Is it an array? An object? Why its both!

  • 7/28/2019 Standard PHP Library

    44/55

    ArrayAccess Interface

  • 7/28/2019 Standard PHP Library

    45/55

    ArrayObjectA class, NOT an interface

    Its like arrayaccess on RedBull

    The manual LIES for a full list of everything you can

    do with arrayobject, seehttp://www.php.net/~helly/php/ext/spl/

    Highlights exchangeArray

    getArrayCopy (get your internally stored array) Sorting methods

    ksort et al

    http://www.php.net/~helly/php/ext/spl/http://www.php.net/~helly/php/ext/spl/
  • 7/28/2019 Standard PHP Library

    46/55

    Countable Interface you can

    implement with anyclass (not iterator

    specific, but used a lotfor it)

    Implement the countmethod and you can use

    the count() PHPfunction on any object

  • 7/28/2019 Standard PHP Library

    47/55

    SplObjectStorage This does not do what you think it does

    Use objects as array keys, uniquely, with no collisionissues (you might get them from spl_object_hash)

    Remember you need the object to get the data backout, 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

    http://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.htmlhttp://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-a-fa.html
  • 7/28/2019 Standard PHP Library

    48/55

    SPLFixedArrayA fixed length, int key only array

    Why? Its FAST (very fast) because it stores datadifferently under the hood in C

    Regular class, dont need to extend and fill in anymethods

    5.3+

  • 7/28/2019 Standard PHP Library

    49/55

    SplObserver and SplSubjectAbstract classes for anything using an observer pattern

    http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/ - greatcommon sense tutorial

    If you do event/observers in your code, extending

    these two makes good sense

    http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/http://www.a-scripts.com/object-oriented-php/2009/02/21/the-observer-pattern/
  • 7/28/2019 Standard PHP Library

    50/55

    SplFileInfo fancy class for a file

    all the file system functions in compact object form

    getMTime == filemtime

    openFile == fopen

  • 7/28/2019 Standard PHP Library

    51/55

    Beyond arrays to the wild wild west

  • 7/28/2019 Standard PHP Library

    52/55

    New and Shiny

    SplDoublyLinkedList

    SplStack SplQueue

  • 7/28/2019 Standard PHP Library

    53/55

    More Data Structures

    SplHeap

    SplMaxHeap SplMinHeap

    SplPriorityQueue

  • 7/28/2019 Standard PHP Library

    54/55

    The Documentation Problem http://elizabethmariesmith.com/2009/02/setting-up-

    phd-on-windows/ - you can write docbook on anyplatform!

    Efnet - #php.doc channel

    http://doc.php.net [email protected] mailing list

    See me and start helping today!

    http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://doc.php.net/mailto:[email protected]:[email protected]://doc.php.net/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/
  • 7/28/2019 Standard PHP Library

    55/55

    Resources http://php.net/spl - spl docs

    http://php.net/~helly/php/ext/spl - Doxygen docs

    http://blueparabola.com/blog/spl-deserves-some-reiteration - some stuff about new data structures

    [email protected]

    http://php.net/splhttp://php.net/~helly/php/ext/splhttp://blueparabola.com/blog/spl-deserves-some-reiterationhttp://blueparabola.com/blog/spl-deserves-some-reiterationmailto:[email protected]:[email protected]://blueparabola.com/blog/spl-deserves-some-reiterationhttp://blueparabola.com/blog/spl-deserves-some-reiterationhttp://blueparabola.com/blog/spl-deserves-some-reiterationhttp://blueparabola.com/blog/spl-deserves-some-reiterationhttp://blueparabola.com/blog/spl-deserves-some-reiterationhttp://blueparabola.com/blog/spl-deserves-some-reiterationhttp://blueparabola.com/blog/spl-deserves-some-reiterationhttp://php.net/~helly/php/ext/splhttp://php.net/spl