SPL Primer
-
Upload
lorna-mitchell -
Category
Technology
-
view
2.018 -
download
5
description
Transcript of SPL Primer
SPL
2
Who am I?
• Lorna Jane Mitchell• PHP consultant, trainer, and writer• Personal site at lornajane.net• Twitter: @lornajane• PHPNW regular!
3
SPL
• Standard PHP Library• An extension to do common things• FAST! Because it's in C• Lots of functionality (tonight is just a
taster)
4
The SPL Drinking Game
• When anyone says "iterator", take a drink
http://www.flickr.com/photos/petereed/2667835909/
5
Before We Go …
• SPL relies heavily on OOP theory and interfaces
• Let's recap on some theory
6
OOP
7
Classes vs Objects
• If the class is the recipe …
• Then the object is the cake
1 <?php 2 3 class CupCake 4 { 5 public function addFrosting($colour, $height) { 6 // ... 7 } 8 } 9 10 $cupcake = new CupCake(); 11 var_dump($cupcake);
http://www.flickr.com/photos/quintanaroo/339856554/
8
Inheritance
• Using classes as "big-picture templates" and changing details
• Parent has shared code• Child contains differences
Cake
BirthdayCake CupCake
9
Example: Cake Class
1 <?php 2 3 class Cake 4 { 5 public function getIngredients() { 6 return array("2 eggs", "margarine", "sugar", "flour"); 7 } 8 }
10
Example: CupCake Class
1 <?php 2 3 class CupCake 4 { 5 public function addFrosting($colour, $height) { 6 // ... 7 } 8 } 9 10 $cupcake = new CupCake(); 11 var_dump($cupcake);
11
Interfaces
• Define a contract• Classes implement interfaces - they must
fulfil the contract• Interfaces define a standard API and
ensure all providers implement it fully
12
Interface Example 1 <?php 2 3 4 interface Audible { 5 function speak(); 6 } 7 8 class Sheep implements Audible { 9 function speak() { 10 echo "baaa!"; 11 } 12 } 13 14 $animal = new Sheep(); 15 if($animal instanceOf Audible) { 16 $animal->speak(); 17 } else { 18 echo "*stares blankly*"; 19 }
Example taken from http://thinkvitamin.com/code/oop-with-php-finishing-touches/
13
Object Comparison
• Check object types– type hinting– instanceOf operator
• Evaluate as true if object either:– is of that type– extends that type– implements that type
14
SPL Interfaces
15
SPL Interfaces
• Countable• ArrayAccess• Iterator
16
Countable
• We can dictate what happens when we count($object)
• Class implements Countable• Class must now include a method called
count()
17
Countable Example 1 <?php 2 3 class Basket implements Countable 4 { 5 public $items = array(); 6 7 public function addItem($item) { 8 $this->items[] = $item; 9 return true; 10 } 11 12 public function count() { 13 return count($this->items); 14 } 15 } 16
http://www.flickr.com/photos/dunbargardens/3740493102/
18
ArrayAccess
• Define object's array behaviour• Allows us to use array notation with an
object in a special way• A good example is SimpleXML, allows
array notation to access attributes
19
ArrayAccess
1 <?xml version="1.0" ?> 2 <foodStore> 3 <fridge> 4 <eggs /> 5 <milk expiry="2010-11-07" /> 6 </fridge> 7 <cupboard> 8 <flour expire="2010-08-31" /> 9 </cupboard> 10 </foodStore>
1 <?php 2 3 $xml = simplexml_load_file('foodstore.xml'); 4 5 echo $xml->fridge->milk['expiry'];
20
Iterators
• Dictate how we traverse an object when looping
• Defines 5 methods:
1 <?php 2 3 interface Iterator 4 { 5 public function key(); 6 public function current(); 7 public function next(); 8 public function valid(); 9 public function rewind(); 10 }
21
More Iterators
• DirectoryIterator• LimitIterator• SimpleXMLIterator• CachingIterator• ...• RecursiveIteratorIterator
22
Using SPL
23
Using SPL
• SPL builds on these ideas to give us great things
• Datastructures• Filesystem handling• SPLException
24
Datastructures
25
SPLFixedArray
• An object that behaves like an array• Fixed length• Integer keys only• FAST!!
26
SPLDoublyLinkedList
• Simple and useful computer science concept
• Ready-to-use implementation• Can push, pop, iterate …
27
SPLStack, SPLQueue
• Built on the SPLDoublyLinkedList• No need to home-spin these solutions in
PHP• Simply extend the SPL ones
28
SPLQueue Example 1 <?php 2 3 class LornaList extends SPLQueue 4 { 5 public function putOnList($item) { 6 $this->enqueue($item); 7 } 8 9 public function getNext() { 10 // FIFO buffer, just gets whatever is oldest 11 $this->dequeue($item); 12 } 13 } 14 15 $todo_list = new LornaList(); 16 17 // procastinate! Write the list 18 $todo_list->putOnList("write SPL talk"); 19 $todo_list->putOnList("wrestle inbox"); 20 $todo_list->putOnList("clean the house"); 21 22 // Time to do stuff 23 $task = $todo_list->getNext(); 24 echo $task;
Filesystem Handling
30
Filesystem Handling
• SPLFileInfo - everything you ever needed to know about a file
• DirectoryIterator - very neat way of working with directories and files
• DirectoryIterator returns individual items of type SPLFileInfo
31
Filesystem Example
• taken directly from devzone– http://devzone.zend.com/article/2565-The-Standard-PHP-Library-SPL
1 <?php 2 3 $path = new DirectoryIterator('/path/to/dir'); 4 5 foreach ($path as $file) { 6 echo $file->getFilename() . "\t"; 7 echo $file->getSize() . "\t"; 8 echo $file->getOwner() . "\t"; 9 echo $file->getMTime() . "\n"; 10 }
32
Exceptions
33
SPLException
• Many additional exception types• Allows us to throw (and catch) specific
exceptions• Including:
– BadMethodCallException– InvalidArgumentException– RuntimeException– OverflowException
34
SPL Exception Example 1 <?php 2 3 class LornaList extends SPLQueue 4 { 5 public function putOnList($item) { 6 if($this->count() > 40) { 7 throw new OverflowException( 8 'Seriously? This is never going to get done' 9 ); 10 } else { 11 $this->enqueue($item); 12 } 13 } 14 15 public function getNext() { 16 // FIFO buffer, just gets whatever is oldest 17 $this->dequeue($item); 18 } 19 } 20
35
SPL
36
SPL
• Interfaces• Datastructures• File handling• Exceptions• … and so much more
37
Questions?
38
Resources
• PHP Manualhttp://uk2.php.net/manual/en/book.spl.php
• DevZone Tutorialhttp://devzone.zend.com/article/2565-The-Standard-PHP-Library-SPL
• PHPPro Articlehttp://www.phpro.org/tutorials/Introduction-to-SPL.html