SPL Primer

38
SPL

description

A quick overview of some but by no means all of the features of the SPL extension

Transcript of SPL Primer

Page 1: SPL Primer

SPL

Page 2: SPL Primer

2

Who am I?

• Lorna Jane Mitchell• PHP consultant, trainer, and writer• Personal site at lornajane.net• Twitter: @lornajane• PHPNW regular!

Page 3: SPL Primer

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)

Page 4: SPL Primer

4

The SPL Drinking Game

• When anyone says "iterator", take a drink

http://www.flickr.com/photos/petereed/2667835909/

Page 5: SPL Primer

5

Before We Go …

• SPL relies heavily on OOP theory and interfaces

• Let's recap on some theory

Page 6: SPL Primer

6

OOP

Page 7: SPL Primer

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/

Page 8: SPL Primer

8

Inheritance

• Using classes as "big-picture templates" and changing details

• Parent has shared code• Child contains differences

Cake

BirthdayCake CupCake

Page 9: SPL Primer

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 }

Page 10: SPL Primer

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);

Page 11: SPL Primer

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

Page 12: SPL Primer

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/

Page 13: SPL Primer

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

Page 14: SPL Primer

14

SPL Interfaces

Page 15: SPL Primer

15

SPL Interfaces

• Countable• ArrayAccess• Iterator

Page 16: SPL Primer

16

Countable

• We can dictate what happens when we count($object)

• Class implements Countable• Class must now include a method called

count()

Page 17: SPL Primer

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/

Page 18: SPL Primer

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

Page 19: SPL Primer

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'];

Page 20: SPL Primer

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 }

Page 21: SPL Primer

21

More Iterators

• DirectoryIterator• LimitIterator• SimpleXMLIterator• CachingIterator• ...• RecursiveIteratorIterator

Page 22: SPL Primer

22

Using SPL

Page 23: SPL Primer

23

Using SPL

• SPL builds on these ideas to give us great things

• Datastructures• Filesystem handling• SPLException

Page 24: SPL Primer

24

Datastructures

Page 25: SPL Primer

25

SPLFixedArray

• An object that behaves like an array• Fixed length• Integer keys only• FAST!!

Page 26: SPL Primer

26

SPLDoublyLinkedList

• Simple and useful computer science concept

• Ready-to-use implementation• Can push, pop, iterate …

Page 27: SPL Primer

27

SPLStack, SPLQueue

• Built on the SPLDoublyLinkedList• No need to home-spin these solutions in

PHP• Simply extend the SPL ones

Page 28: SPL Primer

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;

Page 29: SPL Primer

Filesystem Handling

Page 30: SPL Primer

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

Page 31: SPL Primer

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 }

Page 32: SPL Primer

32

Exceptions

Page 33: SPL Primer

33

SPLException

• Many additional exception types• Allows us to throw (and catch) specific

exceptions• Including:

– BadMethodCallException– InvalidArgumentException– RuntimeException– OverflowException

Page 34: SPL Primer

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

Page 35: SPL Primer

35

SPL

Page 36: SPL Primer

36

SPL

• Interfaces• Datastructures• File handling• Exceptions• … and so much more

Page 37: SPL Primer

37

Questions?

Page 38: SPL Primer

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