Intro to The PHP SPL

download Intro to The PHP SPL

If you can't read please download the document

description

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

Transcript of Intro to The PHP SPL

  • 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 .phpCompleted in 3.4125499725342 seconds [chris@paladine spl]$ php arrayobject.phpCompleted in 3.4321970939636 seconds [chris@paladine spl]$ php fixedarray.phpCompleted in 2.9916169643402 seconds [chris@paladine spl]$ php ll.phpCompleted in 6.5478150844574 seconds [chris@paladine spl]$ php stack.phpCompleted in 6.553768157959 seconds [chris@paladine spl]$ php queue.phpCompleted 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 tothe valid element only http://commons.wikimedia.org/wiki/File:Max-heap.png 32. SplMaxHeap

  • Returns values in Highest->Lowest order