Your SlideShare is downloading. ×
Intro to The PHP SPL
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Intro to The PHP SPL

6,793

Published on

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

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

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

No Downloads
Views
Total Views
6,793
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
86
Comments
0
Likes
12
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Intro to the SPL
      Chris Tankersley
    Joind.in Link: http://joind.in/2477 E-mail: [email_address] Twitter: @dragonmantank
  • 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. The Standard PHP Library
    • Collection of Classes and Interfaces to solve problems and create a standard data access interface
    • 4. Iterators, ArrayObjects, Exceptions, Observer/Subject Pattern
    • 5. Added to PHP in 5.0
    • 6. 5.3 has it mandatory
  • 7. Arrays
  • 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. Why Use This?
    • Array syntax is a core PHP concept
    • 10. On-par with Arrays in Performance
    • 11. Add business logic to Arrays
    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. Implementation
    • Can get away with ArrayAccess, Countable
    • 13. Extend ArrayObject
  • 14. Fixed Arrays
  • 15. SplFixedArray
    • Great when the number of elements is known beforehand
    • 16. Keys must be integers
    • 17. Slightly faster than Arrays or ArrayObject
      • 1,000,000 values inserted and retrieved in 3.2 seconds versus 3.5-3.6 seconds for Array/ArrayObject
  • 18. Lists, Stacks and Queues (Oh My)
  • 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. SplDoublyLinkedList Modes!
    • Can run as a Stack (LIFO)
    • 21. Can run as a Queue (FIFO)
    • 22. Can keep elements over iteration
    • 23. Can delete elements over iteration
  • 24. SplStack
    • Builds upon SplDoublyLinkedList
    • 25. Enforces LIFO
  • 26. SplQueue
    • Builds upon SplDoublyLinkedList
    • 27. Enforces FIFO
  • 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. Heaps, Big and Small
  • 30. Whats a Heap?
    • A tree-based structure that makes finding information more efficient
    • 31. Always have access to the valid element only
    http://commons.wikimedia.org/wiki/File:Max-heap.png
  • 32. SplMaxHeap
    • Returns values in Highest->Lowest order
    <?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. SplMinHeap
    • Does the opposite of the last slide
  • 34. Why use heaps?
    • (Way) More efficient than sorting
    [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. Priority Queues
  • 36. SplPriorityQueue
    • Ignore the name, it's actually a Heap internally
      • SplPriorityHeap doesn't have as nice a ring
    • Allows inserting an object with an associated priority
    • 37. Useful when order needs to be enforced based on some external condition
  • 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. Maps and Sets
  • 40. SplObjectStorage
    • Allows using Objects as array keys
    • 41. Can be used as a Set or a Map
    • 42. The Object is the thing that matters, not the data
    • 43. Can use either Object or Array notation
  • 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. 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. How is this useful?
    • Sets of info keyed by objects
    $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. Iterators!
  • 48. Iterators – Traversing Stuff
    • AppendIterator — The AppendIterator class
    • 49. ArrayIterator — The ArrayIterator class
    • 50. CachingIterator — The CachingIterator class
    • 51. DirectoryIterator — The DirectoryIterator class
    • 52. EmptyIterator — The EmptyIterator class
    • 53. FilesystemIterator — The FilesystemIterator class
    • 54. FilterIterator — The FilterIterator class
    • 55. GlobIterator — The GlobIterator class
    • 56. InfiniteIterator — The InfiniteIterator class
    • 57. IteratorIterator — The IteratorIterator class
    • 58. LimitIterator — The LimitIterator class
    • 59. MultipleIterator — The MultipleIterator class
    • 60. NoRewindIterator — The NoRewindIterator class
    • 61. ParentIterator — The ParentIterator class
    • 62. RecursiveArrayIterator — The RecursiveArrayIterator class
    • 63. RecursiveCachingIterator — The RecursiveCachingIterator class
    • 64. RecursiveDirectoryIterator — The RecursiveDirectoryIterator class
    • 65. RecursiveFilterIterator — The RecursiveFilterIterator class
    • 66. RecursiveIteratorIterator — The RecursiveIteratorIterator class
    • 67. RecursiveRegexIterator — The RecursiveRegexIterator class
    • 68. RecursiveTreeIterator — The RecursiveTreeIterator class
    • 69. RegexIterator — The RegexIterator class
    • 70. SimpleXMLIterator — The SimpleXMLIterator class
  • 71. New Exceptions
  • 72. Why more Exceptions?
    • Less code you have to ship with yours
    • 73. Better error handling in your own code
      • Why have to look for 10 different 'OutOfRange' exceptions?
    • 13 common exceptions are now available
  • 74. Built-In Observer Pattern
  • 75. SplObserver + SplSubject
    • These are just interfaces
    • 76. Subjects can attach Observers and then notify them
    • 77. Very basic implementation
  • 78. Working with Files
  • 79. SplFileObject
    • Allows an OO interface to working with files
    • 80. SplTempFileObject for Temporary Files
    • 81. Can get File Info from a file
    • 82. Makes your life easier
    $file = new SplFileObject ( &quot;myfile.txt&quot; ); foreach ( $file as $line ) { echo $line ; }
  • 83. Questions?
  • 84. Thanks To
    • Matthew Turland
    • 85. Matthew Weier O'Phinney

×