Weitere ähnliche Inhalte Ähnlich wie Intro to The PHP SPL (20) Mehr von Chris Tankersley (20) Kürzlich hochgeladen (20) Intro to The PHP SPL2. 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 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' ]; 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 ; } } 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 ; } 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 31. Always have access to the valid element only http://commons.wikimedia.org/wiki/File:Max-heap.png 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 (); } } } 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 )); 82. Makes your life easier $file = new SplFileObject ( "myfile.txt" ); foreach ( $file as $line ) { echo $line ; }