Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Intro to the SPL <ul>Chris Tankersley </ul>Joind.in Link:  http://joind.in/2477 E-mail:  [email_address] Twitter:  @dragon...
Who Are You and Why Are You In My House? Chris Tankersley Been doing PHP for almost 8 years now Lots of projects no one us...
The Standard PHP Library <ul><li>Collection of Classes and Interfaces to solve problems and create a standard data access ...
Iterators, ArrayObjects, Exceptions, Observer/Subject Pattern
Added to PHP in 5.0
5.3 has it mandatory </li></ul>
Arrays
Doesn't PHP Have Arrays? Yes! $array   =   new array ( 'scalar' ,   'data'   =>   'this!' ); $array2   =   new array (); $...
Why Use This? <ul><li>Array syntax is a core PHP concept
On-par with Arrays in Performance
Add business logic to Arrays </li></ul>class   NumericContainer   implements   ArrayAccess   { protected   $container   = ...
Implementation <ul><li>Can get away with ArrayAccess, Countable
Extend ArrayObject </li></ul>
Fixed Arrays
SplFixedArray <ul><li>Great when the number of elements is known beforehand
Keys must be integers
Slightly faster than Arrays or ArrayObject </li><ul><li>1,000,000 values inserted and retrieved in 3.2 seconds versus 3.5-...
Lists, Stacks and Queues (Oh My)
SplDoublyLinkedList At first glance, why? $list   =   new   SplDoublyLinkedList (); $list -> push ( 1 ); $list -> push ( 2...
SplDoublyLinkedList Modes! <ul><li>Can run as a Stack (LIFO)
Can run as a Queue (FIFO)
Can keep elements over iteration
Can delete elements over iteration </li></ul>
SplStack <ul><li>Builds upon SplDoublyLinkedList
Enforces LIFO </li></ul>
SplQueue <ul><li>Builds upon SplDoublyLinkedList
Enforces FIFO </li></ul>
But, it's slow [chris@paladine spl]$ php   array .php  Completed in   3.4125499725342   seconds [chris@paladine spl]$ php ...
Heaps, Big and Small
Whats a Heap? <ul><li>A tree-based structure that makes finding information more efficient
Always have access to  the valid element only </li></ul>http://commons.wikimedia.org/wiki/File:Max-heap.png
SplMaxHeap <ul><li>Returns values in Highest->Lowest order </li></ul><?php $heap   =   new   SplMaxHeap (); $heap -> inser...
SplMinHeap <ul><li>Does the opposite of the last slide </li></ul>
Why use heaps? <ul><li>(Way) More efficient than sorting </li></ul>[chris@paladine spl]$ php   array -sort-max.php  Comple...
Priority Queues
SplPriorityQueue <ul><li>Ignore the name, it's actually a Heap internally </li><ul><li>SplPriorityHeap doesn't have as nic...
Useful when order needs to be enforced based on some external condition </li></ul>
<?php class   CronHandler   { protected   $queue ; public class   __construct () { $this ->queue   =   new   SplPriorityQu...
Upcoming SlideShare
Loading in …5
×

Intro to The PHP SPL

8,086 views

Published on

A brief introduction to the Standard PHP Library's new objects and programming designs

Published in: Technology
  • Be the first to comment

Intro to The PHP SPL

  1. 1. Intro to the SPL <ul>Chris Tankersley </ul>Joind.in Link: http://joind.in/2477 E-mail: [email_address] Twitter: @dragonmantank
  2. 2. Who Are You and Why Are You In My House? Chris Tankersley Been doing PHP for almost 8 years now Lots of projects no one uses released under the BSD license Contributer to the Habari Project
  3. 3. The Standard PHP Library <ul><li>Collection of Classes and Interfaces to solve problems and create a standard data access interface
  4. 4. Iterators, ArrayObjects, Exceptions, Observer/Subject Pattern
  5. 5. Added to PHP in 5.0
  6. 6. 5.3 has it mandatory </li></ul>
  7. 7. Arrays
  8. 8. Doesn't PHP Have Arrays? Yes! $array = new array ( 'scalar' , 'data' => 'this!' ); $array2 = new array (); $array2 [ 'key' ] = 'value' ; $array2 [] = 'newvalue' ]; Now you can make Objects work like Arrays $object = new MyArrayObject (); $object [] = 'newvalue' ; echo $object [ 'mykey' ];
  9. 9. Why Use This? <ul><li>Array syntax is a core PHP concept
  10. 10. On-par with Arrays in Performance
  11. 11. Add business logic to Arrays </li></ul>class NumericContainer implements ArrayAccess { protected $container = array (); public function offsetSet ( $offset , $value ) { if (! is_numeric ( $value ) { throw new Exception ( 'Value must be numeric!' ); } $this ->container[ $offset ] = $value ; } }
  12. 12. Implementation <ul><li>Can get away with ArrayAccess, Countable
  13. 13. Extend ArrayObject </li></ul>
  14. 14. Fixed Arrays
  15. 15. SplFixedArray <ul><li>Great when the number of elements is known beforehand
  16. 16. Keys must be integers
  17. 17. Slightly faster than Arrays or ArrayObject </li><ul><li>1,000,000 values inserted and retrieved in 3.2 seconds versus 3.5-3.6 seconds for Array/ArrayObject </li></ul></ul>
  18. 18. Lists, Stacks and Queues (Oh My)
  19. 19. SplDoublyLinkedList At first glance, why? $list = new SplDoublyLinkedList (); $list -> push ( 1 ); $list -> push ( 2 ); $list -> push ( 3 ); foreach ( $list as $key => $value ) { echo $key . ': ' . $value ; } $array = array (); $array [] = 1 ; $array [] = 2 ; $array [] = 3 ; foreach ( $array as $key => $value ) { echo $key . ': ' . $value ; }
  20. 20. SplDoublyLinkedList Modes! <ul><li>Can run as a Stack (LIFO)
  21. 21. Can run as a Queue (FIFO)
  22. 22. Can keep elements over iteration
  23. 23. Can delete elements over iteration </li></ul>
  24. 24. SplStack <ul><li>Builds upon SplDoublyLinkedList
  25. 25. Enforces LIFO </li></ul>
  26. 26. SplQueue <ul><li>Builds upon SplDoublyLinkedList
  27. 27. Enforces FIFO </li></ul>
  28. 28. But, it's slow [chris@paladine spl]$ php array .php Completed in 3.4125499725342 seconds [chris@paladine spl]$ php arrayobject.php Completed in 3.4321970939636 seconds [chris@paladine spl]$ php fixedarray.php Completed in 2.9916169643402 seconds [chris@paladine spl]$ php ll.php Completed in 6.5478150844574 seconds [chris@paladine spl]$ php stack.php Completed in 6.553768157959 seconds [chris@paladine spl]$ php queue.php Completed in 6.6605279445648 seconds
  29. 29. Heaps, Big and Small
  30. 30. Whats a Heap? <ul><li>A tree-based structure that makes finding information more efficient
  31. 31. Always have access to the valid element only </li></ul>http://commons.wikimedia.org/wiki/File:Max-heap.png
  32. 32. SplMaxHeap <ul><li>Returns values in Highest->Lowest order </li></ul><?php $heap = new SplMaxHeap (); $heap -> insert ( 5 ); $heap -> insert ( 4 ); $heap -> insert ( 100 ); foreach ( $heap as $value ) { echo $value . &quot; n &quot; ; } [chris@paladine spl]$ php maxheap.php 100 5 4
  33. 33. SplMinHeap <ul><li>Does the opposite of the last slide </li></ul>
  34. 34. Why use heaps? <ul><li>(Way) More efficient than sorting </li></ul>[chris@paladine spl]$ php array -sort-max.php Completed in 8.392333984375E-5 seconds [chris@paladine spl]$ php maxheap-sort.php Completed in 5.6028366088867E-5 seconds
  35. 35. Priority Queues
  36. 36. SplPriorityQueue <ul><li>Ignore the name, it's actually a Heap internally </li><ul><li>SplPriorityHeap doesn't have as nice a ring </li></ul><li>Allows inserting an object with an associated priority
  37. 37. Useful when order needs to be enforced based on some external condition </li></ul>
  38. 38. <?php class CronHandler { protected $queue ; public class __construct () { $this ->queue = new SplPriorityQueue (); } public class addJob (Cron_Job $job ) { $this ->queue-> insert ( $job , $job ->priority); } public class run () { foreach ( $this ->queue as $job ) { $job -> run (); } } }
  39. 39. Maps and Sets
  40. 40. SplObjectStorage <ul><li>Allows using Objects as array keys
  41. 41. Can be used as a Set or a Map
  42. 42. The Object is the thing that matters, not the data
  43. 43. Can use either Object or Array notation </li></ul>
  44. 44. SplObjectStorage as a Set $set = new SplObjectStorage (); $o1 = new StdClass (); $o2 = new StdClass (); $o3 = new StdClass (); $set -> attach ( $o1 ); $set -> attach ( $o2 ); $set -> attach ( $o3 );
  45. 45. SplObjectStorage as a Map $set = new SplObjectStorage (); $o1 = new StdClass (); $o2 = new StdClass (); $o3 = new StdClass (); $set -> attach ( $o1 , array ( 1 , 2 , 3 )); $set -> attach ( $o2 , array ( 4 , 5 , 6 )); $set -> attach ( $o3 , array ( 7 , 8 , 9 ));
  46. 46. How is this useful? <ul><li>Sets of info keyed by objects </li></ul>$user = new User ( $userData ); $user_groups = $groupRepo -> findGroups ( $user ); $groups = new SplObjectStorage (); $groups [ $user ] = $user_groups ; if ( $groups -> offsetExists ( $user )) { return $groups [ $user ]; } else { // Do lookup and cache the result return $groups [ $user ]; }
  47. 47. Iterators!
  48. 48. Iterators – Traversing Stuff <ul><li>AppendIterator — The AppendIterator class
  49. 49. ArrayIterator — The ArrayIterator class
  50. 50. CachingIterator — The CachingIterator class
  51. 51. DirectoryIterator — The DirectoryIterator class
  52. 52. EmptyIterator — The EmptyIterator class
  53. 53. FilesystemIterator — The FilesystemIterator class
  54. 54. FilterIterator — The FilterIterator class
  55. 55. GlobIterator — The GlobIterator class
  56. 56. InfiniteIterator — The InfiniteIterator class
  57. 57. IteratorIterator — The IteratorIterator class
  58. 58. LimitIterator — The LimitIterator class
  59. 59. MultipleIterator — The MultipleIterator class
  60. 60. NoRewindIterator — The NoRewindIterator class
  61. 61. ParentIterator — The ParentIterator class
  62. 62. RecursiveArrayIterator — The RecursiveArrayIterator class
  63. 63. RecursiveCachingIterator — The RecursiveCachingIterator class
  64. 64. RecursiveDirectoryIterator — The RecursiveDirectoryIterator class
  65. 65. RecursiveFilterIterator — The RecursiveFilterIterator class
  66. 66. RecursiveIteratorIterator — The RecursiveIteratorIterator class
  67. 67. RecursiveRegexIterator — The RecursiveRegexIterator class
  68. 68. RecursiveTreeIterator — The RecursiveTreeIterator class
  69. 69. RegexIterator — The RegexIterator class
  70. 70. SimpleXMLIterator — The SimpleXMLIterator class </li></ul>
  71. 71. New Exceptions
  72. 72. Why more Exceptions? <ul><li>Less code you have to ship with yours
  73. 73. Better error handling in your own code </li><ul><li>Why have to look for 10 different 'OutOfRange' exceptions? </li></ul><li>13 common exceptions are now available </li></ul>
  74. 74. Built-In Observer Pattern
  75. 75. SplObserver + SplSubject <ul><li>These are just interfaces
  76. 76. Subjects can attach Observers and then notify them
  77. 77. Very basic implementation </li></ul>
  78. 78. Working with Files
  79. 79. SplFileObject <ul><li>Allows an OO interface to working with files
  80. 80. SplTempFileObject for Temporary Files
  81. 81. Can get File Info from a file
  82. 82. Makes your life easier </li></ul>$file = new SplFileObject ( &quot;myfile.txt&quot; ); foreach ( $file as $line ) { echo $line ; }
  83. 83. Questions?
  84. 84. Thanks To <ul><li>Matthew Turland
  85. 85. Matthew Weier O'Phinney </li></ul>

×