0

      
       An Introduction to  SPL ,  
       the  S tandard  P HP  L ibrary 
      
     
      
       
      
     ...

      
       What is SPL? 
       
       A collection of  classes and interfaces  for   solving  common programming pro...

      
       Why use SPL? 
       <ul>
        
         <ul>
          
           <li>Helps you write  less code 
    ...

      
       SPL Documentation 
       <ul>
        
         <li>Some info here: 
         <ul>
          
           <...

      
       SPL Data Structures 
       <ul>
        
         <ul>
          
           <li>In PHP 5.2 : 
           ...

      
       Iterators 
       <ul>
        
         <li>What's an Iterator? 
        
       </ul>
       An  iterator...

      
       Without Iterators 
      
     
      
       ... 
      
     
      
       ... 
      
     
      
    ...

      
       With Iterators 
      
     
      
       ... 
      
     
      
       Collections: 
      
     
     ...

      
       Iterators in PHP 
       <ul>
        
         <li>To create your own iterator class, implement  Iterator ...

      
       Iterators in PHP 
       
      
     
      
       class  MyList 
       implements  IteratorAggregate { ...

      
       Iterators in PHP 
       Traversable objects get  special foreach treatment : 
      
     
      
       f...

      
       SPL Iterators 
       
      
     
      
       Iterator decorators: 
       AppendIterator 
       Cachi...

      
       More Iterators... 
       <ul>
        
         <li>Many SPL data structures  implement Iterator 
        ...

      
       A Note on Recursive Iterators... 
       <ul>
        
         <li>This doesn't work: 
         
         ...

      
       SPL Iterators 
       
      
     
      
       Iterator decorators: 
       AppendIterator 
       Cachi...

      
       SPL Exceptions 
      
     
      
       
      
     
      
       <ul>
        
         <li>SPL excep...

      
       More SPL Stuff... 
       <ul>
        
         <li>Observer Pattern 
         <ul>
          
           ...

      
       Testing SPL 
       <ul>
        
         <li>Great way to  learn (more) about SPL ! 
         
        
 ...

      
       Come to TestFest! 
       <ul>
        
         Saturday 9 th  May 2009 @ IBM SouthBank 
        
       <...

      
       Iterators – bonus 
       What's this Traversable interface all about? 
       <ul>
        
         <li>I...
Upcoming SlideShare
Loading in...5
×

An Introduction to SPL, the Standard PHP Library

6,963

Published on

An overview of SPL, presented at the PHP London user group on 7th May 2009, in preparation for TestFest (http://phplondon.org/wiki/May_9th_2009).

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,963
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
144
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • Transcript of "An Introduction to SPL, the Standard PHP Library"

    1. 1. An Introduction to SPL , the S tandard P HP L ibrary Robin Fernandes ( [email_address] / @rewbs )
    2. 2. What is SPL? A collection of classes and interfaces for solving common programming problems . <ul> <ul> <ul> <li>Useful data structures <li>Loads of iterators <li>General-purpose exception hierarchy <li>& more... </ul> </ul> </ul>
    3. 3. Why use SPL? <ul> <ul> <li>Helps you write less code <li>Provides reusable components <li>Provides recognisable behaviour <li>It's relatively fast <li>It's well tested (soon to be extremely well tested) </ul> </ul> Zend Framework Agavi
    4. 4. SPL Documentation <ul> <li>Some info here: <ul> <ul> <li>http://php.net/spl </ul> </ul> <li>More info here: <ul> <ul> <li>http://php.net/~helly </ul> </ul> <li>Also see PHP code in cvs: <ul> <ul> <li>ext/spl/examples/*.php, *.inc <li>ext/spl/internal/*.php, *.inc </ul> </ul> </ul>
    5. 5. SPL Data Structures <ul> <ul> <li>In PHP 5.2 : <ul> <li>ArrayObject -> OO array wrapper – extend it! <li>SplObjectStorage -> Dictionary with objects as keys <li>SplFileObject -> OO stream wrapper </ul> <li>New in PHP 5.3 : <ul> <li>SplDoublyLinkedList <li>SplStack -> Last In First Out <li>SplHeap -> Binary heap <ul> <li>SplMaxHeap, SplMinHeap </ul> <li>SplQueue -> First In First Out <li>SplPriorityQueue <li>SplFixedArray -> aka SplFastArray: C array of zval </ul> </ul> </ul>
    6. 6. Iterators <ul> <li>What's an Iterator? </ul> An iterator implements a consistent interface for traversing a collection . <ul> </ul> <ul> <li>Why Use Iterators? </ul> Iterators decouple collections from algorithms by encapsulating traversal logic.
    7. 7. Without Iterators ... ... Algorithms: ... ... ... ... ... ... Sum: Average: Display: Collections: 1 2 3 4 1 4 5 3 2
    8. 8. With Iterators ... Collections: Algorithms: ... Iterators: Algorithms are decoupled from collections! 1 2 3 4 1 4 5 3 2
    9. 9. Iterators in PHP <ul> <li>To create your own iterator class, implement Iterator : </ul> <ul> <li>To make a collection class iterable using a distinct iterator, implement IteratorAggregate : </ul> interface Iterator extends Traversable { function rewind(); function valid(); function current(); function key(); function next(); } interface IteratorAggregate extends Traversable { function getIterator(); }
    10. 10. Iterators in PHP class MyList implements IteratorAggregate { private $nodes = ...; function getIterator() { return new MyListIterator( $this ); } } class MyGraph implements IteratorAggregate { private $nodes = ...; private $edges = ...; function getIterator() { return new MyGraphIterator( $this ); } } function show( $collection ) { foreach ( $collection as $node ) { echo &quot; $node <br/>&quot; ; } } echo show( new MyList); echo show( new MyGraph);
    11. 11. Iterators in PHP Traversable objects get special foreach treatment : foreach ( $traversableObj as $key => $value ) { // Loop body } // 1. Get the Iterator instance from $traversableObj: if ( $traversableObj instanceof Iterator) { $iterator = $traversableObj ; } else if ( $traversableObj instanceof IteratorAggregate){ $iterator = $traversableObj ->getIterator(); } // 2. Loop using the methods provided by Iterator: $iterator ->rewind(); while ( $iterator ->valid()) { $value = $iterator ->current(); $key = $iterator ->key(); // Loop body $iterator ->next(); } <=> … is equivalent to ... Note: <ul> - User-space classes can't implement Traversable directly. </ul> - No class can implement both Iterator and IteratorAggregate.
    12. 12. SPL Iterators Iterator decorators: AppendIterator CachingIterator FilterIterator InfiniteIterator IteratorIterator LimitIterator MultipleIterator (5.3) NoRewindIterator ParentIterator RegexIterator RecursiveIteratorIterator RecursiveCachingIterator RecursiveFilterIterator RecursiveRegexIterator RecursiveTreeIterator (5.3) Concrete iterators: ArrayIterator RecursiveArrayIterator DirectoryIterator RecursiveDirectoryIterator EmptyIterator FilesystemIterator (5.3) GlobIterator (5.3) Unifying interfaces: RecursiveIterator SeekableIterator OuterIterator
    13. 13. More Iterators... <ul> <li>Many SPL data structures implement Iterator <li>-> They can be passed directly to decorators: <li>More iterators implemented in PHP in CVS: <ul> <li>ext/spl/examples/*.inc, *.php <li>For example: callbackFilterIterator.inc <ul> <li>Implementation of FilterIterator which uses a callback (or closure) as the filter function. </ul> </ul> </ul> $s = new SplStack(); $i = new InfiniteIterator ( $s ); $s ->push( 1 ); $s ->push( 2 ); $s ->push( 3 ); foreach ( $i as $v ) { echo &quot; $v &quot; ; //3 2 1 3 2 1 3 2 1 3 2... }
    14. 14. A Note on Recursive Iterators... <ul> <li>This doesn't work: <li>Try this instead: <ul> <li>RecursiveFilterIterator, RecursiveDirectoryIterator etc... implement recursive behaviour but do not exploit it . <ul> <li>-> Perhaps they should be called “recursible” iterators? </ul> <li>A RecursiveIteratorIterator wraps a “recursible” iterators and exploits its recursive behaviour. </ul> </ul> $a = array ( 1 , 2 , array ( 3 , 4 )); $i = new RecursiveArrayIterator( $a ); foreach ( $i as $v ) { echo &quot; $v &quot; ; } // 1 2 Array $a = array ( 1 , 2 , array ( 3 , 4 )); $i = new RecursiveArrayIterator( $a ); $i = new RecursiveIteratorIterator ( $i ); foreach ( $i as $v ) { echo &quot; $v &quot; ; } // 1 2 3 4
    15. 15. SPL Iterators Iterator decorators: AppendIterator CachingIterator FilterIterator InfiniteIterator IteratorIterator LimitIterator MultipleIterator (5.3) NoRewindIterator ParentIterator RegexIterator RecursiveIteratorIterator RecursiveCachingIterator RecursiveFilterIterator RecursiveRegexIterator RecursiveTreeIterator (5.3) Concrete iterators: ArrayIterator RecursiveArrayIterator DirectoryIterator RecursiveDirectoryIterator EmptyIterator FilesystemIterator (5.3) GlobIterator (5.3) Unifying interfaces: RecursiveIterator SeekableIterator OuterIterator
    16. 16. SPL Exceptions <ul> <li>SPL exceptions provide no extra logic or behaviour. </ul> <ul> <li>Each one is associated with a scenario by convention . </ul> <ul> <li>Extend them to specialize them for your domain . </ul> <ul> </ul> Benefits: -> Improve consistency of exception use within & across projects. -> Increase the self-documenting nature of your code.
    17. 17. More SPL Stuff... <ul> <li>Observer Pattern <ul> <li>Simple interfaces: SplObserver, SplSubject </ul> <li>Autoload Enhancements <ul> <li>Default autoloader with extension filtering <li>Define and chain custom autoload callbacks </ul> <li>Utility Functions <ul> <li>iterator_apply () , iterator_count () , iterator_to_array () <li>class_implements () , class_parents () <li>spl_object_hash () </ul> </ul>
    18. 18. Testing SPL <ul> <li>Great way to learn (more) about SPL ! <li>See current code coverage : <ul> <li>http://gcov.php.net/PHP_5_3/lcov_html/spl <ul> </ul> </ul> <li>All tests are valuable! <ul> <ul> <li>Tests for basic functionality <li>Tests for interesting edge cases </ul> </ul> </ul>
    19. 19. Come to TestFest! <ul> Saturday 9 th May 2009 @ IBM SouthBank </ul> <ul> <ul> <ul> <li>Never used SPL? Come to learn! <li>Already know SPL? Come to to dig deeper! <li>Make PHP better! <li>Get kudos! <li>Win an elephpant! </ul> </ul> </ul>
    20. 20. Iterators – bonus What's this Traversable interface all about? <ul> <li>Iterator and IteratorAggregate both extend Traversable . <li>Traversable is a marker interface (it does not provide any methods). <li>It is used internally by the engine to detect iterability. <li>It cannot be implemented directly from PHP code – only indirectly, via Iterator and IteratorAggregate <li>But it can be implemented directly by internal classes to provide limited iterability, without implementing the full Iterator interface . <li>Few people need worry about it. </ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×