7. 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. 8
Inheritance
• Using classes as "big-picture templates"
and changing details
• Parent has shared code
• Child contains differences
Cake
BirthdayCake CupCake
9. 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. 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. 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. 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. 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
16. 16
Countable
• We can dictate what happens when we
count($object)
• Class implements Countable
• Class must now include a method called
count()
17. 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. 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
20. 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 }
30. 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. 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 }