If you can't read please download the document
Upload
chris-tankersley
View
6.838
Download
1
Embed Size (px)
DESCRIPTION
A brief introduction to the Standard PHP Library's new objects and programming designs
Citation preview
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
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?
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
13. Extend ArrayObject 14. Fixed Arrays 15. SplFixedArray
16. Keys must be integers 17. Slightly faster than Arrays or 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!
21. Can run as a Queue (FIFO) 22. Can keep elements over iteration 23. Can delete elements over iteration 24. SplStack
25. Enforces LIFO 26. SplQueue
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?
31. Always have access tothe valid element only http://commons.wikimedia.org/wiki/File:Max-heap.png 32. SplMaxHeap