SlideShare ist ein Scribd-Unternehmen logo
1 von 57
SPL, not a bridge too far
 phpNW 2009 - Manchester
Who am I ?
Michelangelo van Dam
Independent Enterprise PHP consultant
Co-founder PHPBenelux UG


@dragonbe

http://dragonbe.com




              2
Who are you ?

    Are you familiar with php.net ?
    Are you familiar with arrays ?
     Are you familiar with OOP ?
Are you familiar with Design Patterns ?




                   3
What is SPL ?
              Standard PHP Library
          interfaces, classes and methods
      solve common development challenges

             Available since PHP 5.0 !!!

As of 5.3 SPL cannot be turned off from the source !


                          4
SPL by Marcus Börger



               Ittteerrrattor




         5
Definition of SPL


SPL provides a huge toolkit that assists you to easily
iterate over a diversity of data structures in a
standardized way




                          6
What does it provide ?
•   ArrayObject - approach arrays as objects
•   Iterators - various iterators
•   Interfaces - iterator interfaces for your objects
•   Exceptions - exceptions to streamline error handling
•   SPL Functions - extra functions and autoloader func
•   SplFileInfo - tools for filesystem access
•   Data structures - structuring data sequences


                             7
<?php phpinfo(); ?>




         8
ArrayObject

•   provides an interface
    - treat arrays as objects
    - elements are iteratable
    - provides serializing and deserializing of arrays
    - sorting elements (w/ or w/o callback methods)
    - exchange elements with other arrays or objects

                             9
<?php
        ArrayObject Example
$myArray = array (
    'time'      => 'Derick Rethans',
    'test'      => 'Sebastian Bergmann',
    'iterate'   => 'Marcus Börger',
);


$obj = new ArrayObject($myArray);
print_r($obj);

$obj->uasort(function ($a, $b) {
  if ($a == $b) {
    return 0;
  }
  return ($a < $b) ? -1 : 1;
});
print_r($obj);



                               10
ArrayObject output
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [time] => Derick Rethans
            [test] => Sebastian Bergmann
            [iterate] => Marcus Börger
        )

)
ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [time] => Derick Rethans
            [iterate] => Marcus Börger
            [test] => Sebastian Bergmann
        )

)


                               11
…
       More of ArrayObject
// serializing object for caching, sessions, …
$obj->serialize();

// adding more key/value elements to the stack
$obj->offsetSet('enterprise', 'Ivo Jansch');

// removing by key
$obj->offsetUnset('time');

…




                               12
Iterator
•   provides a common interface
•   to iterate over “things”
    - xml data
    - database data
    - arrays
•   move back and forth in a stack
•   distinct methods to access keys and values
•   specific iterators for different purposes


                             13
Advantage ?

•   Reusable code
    - data structures can change
    - object oriented
      ✓ extending
      ✓ refactoring
      ✓ overloading



                            14
Example


•   retrieve data in an array
•   with items filtered out




                                15
<?php
        FilterIterator Example
class filterOut extends FilterIterator
{
    private $_filter;

    public function __construct(Iterator $it, $filter)
    {
       parent::__construct($it);
       $this->_filter = $filter;
    }

    public function accept()
    {
        $key = $this->getInnerIterator()->key();
        return ($key == $this->_filter) ? false : true;
    }
}




                               16
<?php
        FilterIterator output
$myArray = array (
    'time'      => 'Derick Rethans',
    'test'      => 'Sebastian Bergmann',
    'iterate'   => 'Marcus Börger',
);
$obj = new ArrayObject($myArray);

$iterator = new filterOut($obj->getIterator(), 'time');

foreach ($iterator as $item) {
  var_dump($item);
}

//ouputs
string(18) "Sebastian Bergmann"
string(13) "Marcus Börger"




                                  17
DirectoryIterator


•   dealing with files and directories
•   looping back and forth between files
•   sorting files w/ or w/o custom sorting algorithms




                            18
Example


•   show an list of archive files
•   ordered with last archive first




                             19
Directory contents
$ ls archives/
datafile-20090901.csv   datafile-20090906.csv
datafile-20090911.csv   datafile-20090916.csv
datafile-20090921.csv   datafile-20090926.csv
datafile-20090902.csv   datafile-20090907.csv
datafile-20090912.csv   datafile-20090917.csv
datafile-20090922.csv   datafile-20090927.csv
datafile-20090903.csv   datafile-20090908.csv
datafile-20090913.csv   datafile-20090918.csv
datafile-20090923.csv   datafile-20090928.csv
datafile-20090904.csv   datafile-20090909.csv
datafile-20090914.csv   datafile-20090919.csv
datafile-20090924.csv   datafile-20090929.csv
datafile-20090905.csv   datafile-20090910.csv
datafile-20090915.csv   datafile-20090920.csv
datafile-20090925.csv   datafile-20090930.csv




                               20
SortableDirectorIterator
<?php
class SortableDirectoryIterator extends DirectoryIterator
{
    public function sortUp()
    {
        $storage = $this->_getStorage();
        $storage->uksort(function ($a, $b) {
            if ($a == $b) return 0;
            return ($a < $b) ? -1 : 1;
        });
        return $storage;
    }
    public function sortDown()
    {
        $storage = $this->_getStorage();
        $storage->uksort(function ($a, $b) {
          if ($a == $b) return 0;
          return ($a < $b) ? 1 : -1;
        });
        return $storage;
    }

                               21
…
    Our Storage Container
    protected function _getStorage()
    {
        $obj = new ArrayObject();
        foreach ($this as $file) {
            if ($file->isDot()) continue;
            $obj->offsetSet($file->getFileName(),
                            $file->getFileInfo());
        }
        return $obj;
    }
}

$dir = new SortableDirectoryIterator('./archives');
$sortObj = $dir->sortDown();
$iterator = $sortObj->getIterator();
while ($iterator->valid()) {
    echo $iterator->current()->getPathName() . PHP_EOL;
    $iterator->next();
}


                               22
Voila ! Descending filenames
./archives/datafile-20090930.csv        ./archives/datafile-20090915.csv
./archives/datafile-20090929.csv        ./archives/datafile-20090914.csv
./archives/datafile-20090928.csv        ./archives/datafile-20090913.csv
./archives/datafile-20090927.csv        ./archives/datafile-20090912.csv
./archives/datafile-20090926.csv        ./archives/datafile-20090911.csv
./archives/datafile-20090925.csv        ./archives/datafile-20090910.csv
./archives/datafile-20090924.csv        ./archives/datafile-20090909.csv
./archives/datafile-20090923.csv        ./archives/datafile-20090908.csv
./archives/datafile-20090922.csv        ./archives/datafile-20090907.csv
./archives/datafile-20090921.csv        ./archives/datafile-20090906.csv
./archives/datafile-20090920.csv        ./archives/datafile-20090905.csv
./archives/datafile-20090919.csv        ./archives/datafile-20090904.csv
./archives/datafile-20090918.csv        ./archives/datafile-20090903.csv
./archives/datafile-20090917.csv        ./archives/datafile-20090902.csv
./archives/datafile-20090916.csv        ./archives/datafile-20090901.csv




                                   23
RecursiveIteratorIterator

•   iterates over existing iterator
•   over multiple levels
•   easy to flatten out nested array structures
•   controlling recursive interactions




                             24
Example
                              Chuck Norris
                             Account Manager



            Jane Doe                              John Doe
         Project Manager                       Project Manager



Cinderella                Shrek
Developer           Graphical Designer




                                         25
RecursiveIteratorIterator
<?php
$company = array (
    array (
        'name' => 'Chuck Norris','position' => 'Account Manager',
        'manages' => array (
            array (
                'name' => 'Jane Doe','position' => 'Project Manager',
                'manages' => array (
                    array (
                        'name' => 'Cinderella','position' => 'Developer',
                        'manages' => array (),
                    ),
                    array (
                        'name' => 'Shrek','position' => 'Graphical Designer',
                        'manages' => array (),
                    ),
                ),
            ),
            array (
                'name' => 'John Doe','position' => 'Project Manager',
                'manages' => array (),
            ),
        ),
    ),
);




                                         26
Flattened Array output
$iterator = new RecursiveArrayIterator(new ArrayObject($company));
$ritit = new RecursiveIteratorIterator($iterator);
foreach ($ritit as $key => $value) {
    echo $key . ' = ' . $value . PHP_EOL;
}


// outputs
name = Chuck Norris
position = Account Manager
name = Jane Doe
position = Project Manager
name = Cinderella
position = Developer
name = Shrek
position = Graphical Designer
name = John Doe
position = Project Manager




                                27
Interfaces

•   Countable: an internal counter
•   OuterIterator: iteration over inner iterators
•   RecursiveIterator: iterating in an recursive way
•   SeekableIterator: an internal stack seeker
•   SplObserver: implements observer pattern
•   SplSubject: implements observer pattern



                             28
<?php
              Interface example
// file: Order.php

class Order implements Countable, SplSubject
{
    protected $_orders;
    protected $_count;

   public function __construct()
   {
       $this->_count = 0;
       $this->_orders = array ();
   }

   public function placeOrder()
   {
       $this->_count++;
   }

   public function attach(SplObserver $observer)
   {
       $this->_orders[] = $observer;
   }

   public function detach(SplObserver $observer)
   {
       // not used in this case
   }


                                         29
Interface Example (2)
    public function notify()
    {
        foreach ($this->_orders as $obj) {
            $obj->update($this);
        }
    }

    public function count()
    {
         return $this->_count;
    }
}

<?php
// file: PlaceOrder.php

class PlaceOrder implements SplObserver
{

    public function update(SplSubject $order)
    {
        echo 'We have ' . count($order) . ' orders now' . PHP_EOL;
    }
}




                                          30
Running Interface Example
<?php

require_once 'Order.php';
require_once 'PlaceOrder.php';

$order = new Order();
$placeOrder = new PlaceOrder();

$order->attach($placeOrder);
$order->notify();

$order->placeOrder();
$order->notify();

$order->placeOrder();
$order->notify();


$ php ./spl_observer.php
We have 0 orders now
We have 1 orders now
We have 2 orders now




                                  31
SPL Exceptions
•   SPL Exceptions
    - templates
    - throw exceptions
    - common issues
•   Types of exceptions
    - LogicExceptions
    - RuntimeExceptions


                          32
SPL LogicException Tree




           33
SPL RuntimeException Tree




            34
<?php
           Exceptions Example
//file: spl_exception01.php
class MyClass
{
    public function giveANumberFromOneToTen($number)
    {
        if($number < 1 || $number > 10) {
            throw new OutOfBoundsException('Number should be between 1 and 10');
        }
        echo $number . PHP_EOL;
    }
}

$my = new MyClass();
try {
    $my->giveANumberFromOneToTen(5);
    $my->giveANumberFromOneToTen(20);
} catch (OutOfBoundsException $e) {
    echo $e->getMessage() . PHP_EOL;
}

Output:
$ /usr/bin/php ./spl_exception01.php
5
Number should be between 1 and 10




                                         35
SplFunctions


•   functions for PHP and SPL in particular
•   often dealing with auto loading
•   some for internal referencing




                             36
<?php
        SplFunctions Example
interface foo {}
interface bar {}

class baz implements foo, bar {}
class example extends baz {}

var_dump(class_implements(new baz));

var_dump(class_implements(new example));




                               37
Output of SplFunctions
array(2) {
  ["foo"]=>
  string(3)   "foo"
  ["bar"]=>
  string(3)   "bar"
}
array(2) {
  ["bar"]=>
  string(3)   "bar"
  ["foo"]=>
  string(3)   "foo"
}




                      38
SPLFileInfo


The SplFileInfo class offers a high-level object oriented
    interface to information for an individual file.




                           39
<?php
         SplFileInfo Example
// use the current file to get information from
$file = new SplFileInfo(dirname(__FILE__));

var_dump($file->isFile());
var_dump($file->getMTime());
var_dump($file->getSize());
var_dump($file->getFileInfo());
var_dump($file->getOwner());



//output
bool(false)
int(1244760945)
int(408)
object(SplFileInfo)#2 (0) {
}
int(501)


                                  40
Processing CSV with SPL
Consider the following data.csv


Derick Rethans;time
Sebastian Bergmann;test
Marcus Börger;iterate
Ivo Jansch;enterprise
Matthew Weier O'Phinney;extend
Michelangelo van Dam;elephpant




                                  41
<?php
          SPL usage on CSV
$info = new SplFileInfo('data.csv');
if ($info->isReadable()) {
    $file = $info->openFile();
    $file->setFlags(SplFileObject::READ_CSV);
    $file->setCsvControl(';','"');
    foreach ($file as $row) {
        list ($user, $term) = $row;
        if (null !== $user && null !== $term) {
            echo $user . ' is known for ' . $term . PHP_EOL;
        }
    }
}

//outputs
Derick Rethans is known for time
Sebastian Bergmann is known for test
Marcus Börger is known for iterate
Ivo Jansch is known for enterprise
Matthew Weier O'Phinney is known for extend
Michelangelo van Dam is known for elephpant

                               42
Data Structures
•   Available in PHP 5.3.x

    -   SplDoublyLinkedList
        ✓ SplStack
        ✓ SplQueue
        ✓ SplHeap
        ✓ SplMaxHeap
        ✓ SplMinHeap
        ✓ SplPriorityQueue

                              43
Data Structures Example
<?php
// file: spl_stack01.php
$stack = new SplStack();
$stack->push('Message 1');
$stack->push('Message 2');
$stack->push('Message 3');

echo $stack->pop() . PHP_EOL;
echo $stack->pop() . PHP_EOL;
echo $stack->pop() . PHP_EOL;

Outputs:
$ /usr/bin/php ./spl_stack01.php
Message 3
Message 2
Message 1




                                44
SplHeap

•   SplHeap is an abstract class
    - SplMinHeap implements SplHeap (low » high)
    - SplMaxHeap implements SplHeap (high » low)
•   stacking values w/o FIFO, FILO order




                           45
Simple SplHeap example
<?php

$heap = new SplMinHeap;
$heap->insert(5);
$heap->insert(2);
$heap->insert(8);
$heap->insert(6);

$heap->top();
while ($heap->valid()) {
    echo $heap->key() . ': ' . $heap->current() . PHP_EOL;
    $heap->next();
}

//outputs
3: 2
2: 5
1: 6
0: 8



                               46
JupilerLeague with SplHeap
<?php
class JupilerLeague extends SplHeap
{
    public function compare($array1, $array2)
    {
        $values1 = array_values($array1);
        $values2 = array_values($array2);
        if ($values1[0] === $values2[0]) return 0;
        return $values1[0] < $values2[0] ? -1 : 1;
    }
}

$heap = new JupilerLeague();
$heap->insert(array ('AA Gent' => 15)); $heap->insert(array ('Anderlecht' => 20));
$heap->insert(array ('Cercle Brugge' => 11)); $heap->insert(array ('Charleroi' => 12));
$heap->insert(array ('Club Brugge' => 21)); $heap->insert(array ('G. Beerschot' => 15));
$heap->insert(array ('Kortrijk' => 10)); $heap->insert(array ('KV Mechelen' => 18));
$heap->insert(array ('Lokeren' => 10)); $heap->insert(array ('Moeskroen' => 7));
$heap->insert(array ('Racing Genk' => 11)); $heap->insert(array ('Roeselare' => 6));
$heap->insert(array ('Standard' => 20)); $heap->insert(array ('STVV' => 17));
$heap->insert(array ('Westerlo' => 10)); $heap->insert(array ('Zulte Waregem' => 15));

$heap->top();
while ($heap->valid()) {
  list ($team, $score) = each ($heap->current());
  echo $team . ': ' . $score . PHP_EOL;
  $heap->next();
}


                                         47
JupilerLeague Scoreboard
Club Brugge: 21
Anderlecht: 20
Standard: 20
KV Mechelen: 18
STVV: 17
Zulte Waregem: 15
AA Gent: 15
G. Beerschot: 15
Charleroi: 12
Racing Genk: 11
Cercle Brugge: 11
Kortrijk: 10
Lokeren: 10
Westerlo: 10
Moeskroen: 7
Roeselare: 6




                    48
Conclusion


SPL can help you solve common PHP issues
        it’s built-in, so why not use it
   it requires no “advanced skills” to use




                    49
SPL is not all good

•   Matthew “Elazar” Turland pointed out:
    - Performance could be better (SPLStack)
    - ArrayObject doesn’t support all array
      functions
•   See his presentation:
    http://ishouldbecoding.com/publications



                            50
Not a bridge too far...




           51
Only one minor thing...




           52
Documentation problem

•   php.net/spl needs more documentation !
•   you can help on that part:
    - see http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows
    - you can set up phpdoc on each platform !
    - Efnet: #php.doc channel
    - http://doc.php.net - phpdoc@php.net

                                    53
More about SPL...
•   main SPL documentation:
    http://php.net/spl
•   PHPro Tutorials on SPL:
    http://www.phpro.org/tutorials/Introduction-to-SPL.html
•   Lorenzo Alberton’s blog:
    http://www.alberton.info/php_5.3_spl_data_structures.html
•   http://www.colder.ch/news/01-08-2009/34/splobjectstorage-for-
    a-fa.html



                                54
License
This presentation is released under the Creative Commons
Attribution-Share Alike 3.0 Unported License
You are free:
- to share : to copy, distribute and transmit the work
- to remix : to adapt the work

Under the following conditions:
- attribution : You must attribute the work in the manner specified by the author or licensor
- share alike : If you alter, transform, or build upon this work, you may distribute the resulting work
only under the same, similar or a compatible license



See: http://creativecommons.org/licenses/by-sa/3.0/



                                                   55
Photo credits

       Marcus Boerger - Sebastian Bergmann
http://www.flickr.com/photos/sebastian_bergmann/173891817

       Golden Gate Bridge - Felix De Vliegher
      http://www.flickr.com/photos/felixdv/2883471022/




                            56
Thank you

        Find my slides on
http://slideshare.net/DragonBe

    Don’t forget to vote !
     http://joind.in/613




            57

Weitere ähnliche Inhalte

Was ist angesagt?

Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Mark Baker
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language TriviaNikita Popov
 
Invertible-syntax 入門
Invertible-syntax 入門Invertible-syntax 入門
Invertible-syntax 入門Hiromi Ishii
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arraysKumar
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
Metaprogramming in Haskell
Metaprogramming in HaskellMetaprogramming in Haskell
Metaprogramming in HaskellHiromi Ishii
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8PrinceGuru MS
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeProf. Wim Van Criekinge
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginnersleo lapworth
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
Solr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg DonovanSolr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg DonovanGregg Donovan
 
Php course-in-navimumbai
Php course-in-navimumbaiPhp course-in-navimumbai
Php course-in-navimumbaivibrantuser
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl courseMarc Logghe
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP ArraysAhmed Swilam
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsPierre MARTIN
 

Was ist angesagt? (20)

Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 
Invertible-syntax 入門
Invertible-syntax 入門Invertible-syntax 入門
Invertible-syntax 入門
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Metaprogramming in Haskell
Metaprogramming in HaskellMetaprogramming in Haskell
Metaprogramming in Haskell
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
 
Bioinformatica p6-bioperl
Bioinformatica p6-bioperlBioinformatica p6-bioperl
Bioinformatica p6-bioperl
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginners
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Solr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg DonovanSolr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg Donovan
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Lithium Best
Lithium Best Lithium Best
Lithium Best
 
Php course-in-navimumbai
Php course-in-navimumbaiPhp course-in-navimumbai
Php course-in-navimumbai
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
Rust ⇋ JavaScript
Rust ⇋ JavaScriptRust ⇋ JavaScript
Rust ⇋ JavaScript
 

Ähnlich wie Spl Not A Bridge Too Far phpNW09

Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Jeff Carouth
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Michelangelo van Dam
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7chuvainc
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?Alexandru Badiu
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Leonardo Proietti
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksNate Abele
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)James Titcumb
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate Abele
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Editionddiers
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)Night Sailer
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redisjimbojsb
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Database API, your new friend
Database API, your new friendDatabase API, your new friend
Database API, your new friendkikoalonsob
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Perforce Object and Record Model
Perforce Object and Record Model  Perforce Object and Record Model
Perforce Object and Record Model Perforce
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressAlena Holligan
 

Ähnlich wie Spl Not A Bridge Too Far phpNW09 (20)

Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Database API, your new friend
Database API, your new friendDatabase API, your new friend
Database API, your new friend
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Perforce Object and Record Model
Perforce Object and Record Model  Perforce Object and Record Model
Perforce Object and Record Model
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
 

Mehr von Michelangelo van Dam

GDPR Art. 25 - Privacy by design and default
GDPR Art. 25 - Privacy by design and defaultGDPR Art. 25 - Privacy by design and default
GDPR Art. 25 - Privacy by design and defaultMichelangelo van Dam
 
Moving from app services to azure functions
Moving from app services to azure functionsMoving from app services to azure functions
Moving from app services to azure functionsMichelangelo van Dam
 
General Data Protection Regulation, a developer's story
General Data Protection Regulation, a developer's storyGeneral Data Protection Regulation, a developer's story
General Data Protection Regulation, a developer's storyMichelangelo van Dam
 
Leveraging a distributed architecture to your advantage
Leveraging a distributed architecture to your advantageLeveraging a distributed architecture to your advantage
Leveraging a distributed architecture to your advantageMichelangelo van Dam
 
Open source for a successful business
Open source for a successful businessOpen source for a successful business
Open source for a successful businessMichelangelo van Dam
 
Decouple your framework now, thank me later
Decouple your framework now, thank me laterDecouple your framework now, thank me later
Decouple your framework now, thank me laterMichelangelo van Dam
 
Deploy to azure in less then 15 minutes
Deploy to azure in less then 15 minutesDeploy to azure in less then 15 minutes
Deploy to azure in less then 15 minutesMichelangelo van Dam
 
Azure and OSS, a match made in heaven
Azure and OSS, a match made in heavenAzure and OSS, a match made in heaven
Azure and OSS, a match made in heavenMichelangelo van Dam
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your projectMichelangelo van Dam
 
PHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the testsPHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the testsMichelangelo van Dam
 
Easily extend your existing php app with an api
Easily extend your existing php app with an apiEasily extend your existing php app with an api
Easily extend your existing php app with an apiMichelangelo van Dam
 

Mehr von Michelangelo van Dam (20)

GDPR Art. 25 - Privacy by design and default
GDPR Art. 25 - Privacy by design and defaultGDPR Art. 25 - Privacy by design and default
GDPR Art. 25 - Privacy by design and default
 
Moving from app services to azure functions
Moving from app services to azure functionsMoving from app services to azure functions
Moving from app services to azure functions
 
Privacy by design
Privacy by designPrivacy by design
Privacy by design
 
DevOps or DevSecOps
DevOps or DevSecOpsDevOps or DevSecOps
DevOps or DevSecOps
 
Privacy by design
Privacy by designPrivacy by design
Privacy by design
 
Continuous deployment 2.0
Continuous deployment 2.0Continuous deployment 2.0
Continuous deployment 2.0
 
Let your tests drive your code
Let your tests drive your codeLet your tests drive your code
Let your tests drive your code
 
General Data Protection Regulation, a developer's story
General Data Protection Regulation, a developer's storyGeneral Data Protection Regulation, a developer's story
General Data Protection Regulation, a developer's story
 
Leveraging a distributed architecture to your advantage
Leveraging a distributed architecture to your advantageLeveraging a distributed architecture to your advantage
Leveraging a distributed architecture to your advantage
 
The road to php 7.1
The road to php 7.1The road to php 7.1
The road to php 7.1
 
Open source for a successful business
Open source for a successful businessOpen source for a successful business
Open source for a successful business
 
Decouple your framework now, thank me later
Decouple your framework now, thank me laterDecouple your framework now, thank me later
Decouple your framework now, thank me later
 
Deploy to azure in less then 15 minutes
Deploy to azure in less then 15 minutesDeploy to azure in less then 15 minutes
Deploy to azure in less then 15 minutes
 
Azure and OSS, a match made in heaven
Azure and OSS, a match made in heavenAzure and OSS, a match made in heaven
Azure and OSS, a match made in heaven
 
Getting hands dirty with php7
Getting hands dirty with php7Getting hands dirty with php7
Getting hands dirty with php7
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your project
 
Create, test, secure, repeat
Create, test, secure, repeatCreate, test, secure, repeat
Create, test, secure, repeat
 
The Continuous PHP Pipeline
The Continuous PHP PipelineThe Continuous PHP Pipeline
The Continuous PHP Pipeline
 
PHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the testsPHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the tests
 
Easily extend your existing php app with an api
Easily extend your existing php app with an apiEasily extend your existing php app with an api
Easily extend your existing php app with an api
 

Kürzlich hochgeladen

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Kürzlich hochgeladen (20)

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

Spl Not A Bridge Too Far phpNW09

  • 1. SPL, not a bridge too far phpNW 2009 - Manchester
  • 2. Who am I ? Michelangelo van Dam Independent Enterprise PHP consultant Co-founder PHPBenelux UG @dragonbe http://dragonbe.com 2
  • 3. Who are you ? Are you familiar with php.net ? Are you familiar with arrays ? Are you familiar with OOP ? Are you familiar with Design Patterns ? 3
  • 4. What is SPL ? Standard PHP Library interfaces, classes and methods solve common development challenges Available since PHP 5.0 !!! As of 5.3 SPL cannot be turned off from the source ! 4
  • 5. SPL by Marcus Börger Ittteerrrattor 5
  • 6. Definition of SPL SPL provides a huge toolkit that assists you to easily iterate over a diversity of data structures in a standardized way 6
  • 7. What does it provide ? • ArrayObject - approach arrays as objects • Iterators - various iterators • Interfaces - iterator interfaces for your objects • Exceptions - exceptions to streamline error handling • SPL Functions - extra functions and autoloader func • SplFileInfo - tools for filesystem access • Data structures - structuring data sequences 7
  • 9. ArrayObject • provides an interface - treat arrays as objects - elements are iteratable - provides serializing and deserializing of arrays - sorting elements (w/ or w/o callback methods) - exchange elements with other arrays or objects 9
  • 10. <?php ArrayObject Example $myArray = array ( 'time' => 'Derick Rethans', 'test' => 'Sebastian Bergmann', 'iterate' => 'Marcus Börger', ); $obj = new ArrayObject($myArray); print_r($obj); $obj->uasort(function ($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); print_r($obj); 10
  • 11. ArrayObject output ArrayObject Object ( [storage:ArrayObject:private] => Array ( [time] => Derick Rethans [test] => Sebastian Bergmann [iterate] => Marcus Börger ) ) ArrayObject Object ( [storage:ArrayObject:private] => Array ( [time] => Derick Rethans [iterate] => Marcus Börger [test] => Sebastian Bergmann ) ) 11
  • 12. More of ArrayObject // serializing object for caching, sessions, … $obj->serialize(); // adding more key/value elements to the stack $obj->offsetSet('enterprise', 'Ivo Jansch'); // removing by key $obj->offsetUnset('time'); … 12
  • 13. Iterator • provides a common interface • to iterate over “things” - xml data - database data - arrays • move back and forth in a stack • distinct methods to access keys and values • specific iterators for different purposes 13
  • 14. Advantage ? • Reusable code - data structures can change - object oriented ✓ extending ✓ refactoring ✓ overloading 14
  • 15. Example • retrieve data in an array • with items filtered out 15
  • 16. <?php FilterIterator Example class filterOut extends FilterIterator { private $_filter; public function __construct(Iterator $it, $filter) { parent::__construct($it); $this->_filter = $filter; } public function accept() { $key = $this->getInnerIterator()->key(); return ($key == $this->_filter) ? false : true; } } 16
  • 17. <?php FilterIterator output $myArray = array ( 'time' => 'Derick Rethans', 'test' => 'Sebastian Bergmann', 'iterate' => 'Marcus Börger', ); $obj = new ArrayObject($myArray); $iterator = new filterOut($obj->getIterator(), 'time'); foreach ($iterator as $item) { var_dump($item); } //ouputs string(18) "Sebastian Bergmann" string(13) "Marcus Börger" 17
  • 18. DirectoryIterator • dealing with files and directories • looping back and forth between files • sorting files w/ or w/o custom sorting algorithms 18
  • 19. Example • show an list of archive files • ordered with last archive first 19
  • 20. Directory contents $ ls archives/ datafile-20090901.csv datafile-20090906.csv datafile-20090911.csv datafile-20090916.csv datafile-20090921.csv datafile-20090926.csv datafile-20090902.csv datafile-20090907.csv datafile-20090912.csv datafile-20090917.csv datafile-20090922.csv datafile-20090927.csv datafile-20090903.csv datafile-20090908.csv datafile-20090913.csv datafile-20090918.csv datafile-20090923.csv datafile-20090928.csv datafile-20090904.csv datafile-20090909.csv datafile-20090914.csv datafile-20090919.csv datafile-20090924.csv datafile-20090929.csv datafile-20090905.csv datafile-20090910.csv datafile-20090915.csv datafile-20090920.csv datafile-20090925.csv datafile-20090930.csv 20
  • 21. SortableDirectorIterator <?php class SortableDirectoryIterator extends DirectoryIterator { public function sortUp() { $storage = $this->_getStorage(); $storage->uksort(function ($a, $b) { if ($a == $b) return 0; return ($a < $b) ? -1 : 1; }); return $storage; } public function sortDown() { $storage = $this->_getStorage(); $storage->uksort(function ($a, $b) { if ($a == $b) return 0; return ($a < $b) ? 1 : -1; }); return $storage; } 21
  • 22. Our Storage Container protected function _getStorage() { $obj = new ArrayObject(); foreach ($this as $file) { if ($file->isDot()) continue; $obj->offsetSet($file->getFileName(), $file->getFileInfo()); } return $obj; } } $dir = new SortableDirectoryIterator('./archives'); $sortObj = $dir->sortDown(); $iterator = $sortObj->getIterator(); while ($iterator->valid()) { echo $iterator->current()->getPathName() . PHP_EOL; $iterator->next(); } 22
  • 23. Voila ! Descending filenames ./archives/datafile-20090930.csv ./archives/datafile-20090915.csv ./archives/datafile-20090929.csv ./archives/datafile-20090914.csv ./archives/datafile-20090928.csv ./archives/datafile-20090913.csv ./archives/datafile-20090927.csv ./archives/datafile-20090912.csv ./archives/datafile-20090926.csv ./archives/datafile-20090911.csv ./archives/datafile-20090925.csv ./archives/datafile-20090910.csv ./archives/datafile-20090924.csv ./archives/datafile-20090909.csv ./archives/datafile-20090923.csv ./archives/datafile-20090908.csv ./archives/datafile-20090922.csv ./archives/datafile-20090907.csv ./archives/datafile-20090921.csv ./archives/datafile-20090906.csv ./archives/datafile-20090920.csv ./archives/datafile-20090905.csv ./archives/datafile-20090919.csv ./archives/datafile-20090904.csv ./archives/datafile-20090918.csv ./archives/datafile-20090903.csv ./archives/datafile-20090917.csv ./archives/datafile-20090902.csv ./archives/datafile-20090916.csv ./archives/datafile-20090901.csv 23
  • 24. RecursiveIteratorIterator • iterates over existing iterator • over multiple levels • easy to flatten out nested array structures • controlling recursive interactions 24
  • 25. Example Chuck Norris Account Manager Jane Doe John Doe Project Manager Project Manager Cinderella Shrek Developer Graphical Designer 25
  • 26. RecursiveIteratorIterator <?php $company = array ( array ( 'name' => 'Chuck Norris','position' => 'Account Manager', 'manages' => array ( array ( 'name' => 'Jane Doe','position' => 'Project Manager', 'manages' => array ( array ( 'name' => 'Cinderella','position' => 'Developer', 'manages' => array (), ), array ( 'name' => 'Shrek','position' => 'Graphical Designer', 'manages' => array (), ), ), ), array ( 'name' => 'John Doe','position' => 'Project Manager', 'manages' => array (), ), ), ), ); 26
  • 27. Flattened Array output $iterator = new RecursiveArrayIterator(new ArrayObject($company)); $ritit = new RecursiveIteratorIterator($iterator); foreach ($ritit as $key => $value) { echo $key . ' = ' . $value . PHP_EOL; } // outputs name = Chuck Norris position = Account Manager name = Jane Doe position = Project Manager name = Cinderella position = Developer name = Shrek position = Graphical Designer name = John Doe position = Project Manager 27
  • 28. Interfaces • Countable: an internal counter • OuterIterator: iteration over inner iterators • RecursiveIterator: iterating in an recursive way • SeekableIterator: an internal stack seeker • SplObserver: implements observer pattern • SplSubject: implements observer pattern 28
  • 29. <?php Interface example // file: Order.php class Order implements Countable, SplSubject { protected $_orders; protected $_count; public function __construct() { $this->_count = 0; $this->_orders = array (); } public function placeOrder() { $this->_count++; } public function attach(SplObserver $observer) { $this->_orders[] = $observer; } public function detach(SplObserver $observer) { // not used in this case } 29
  • 30. Interface Example (2) public function notify() { foreach ($this->_orders as $obj) { $obj->update($this); } } public function count() { return $this->_count; } } <?php // file: PlaceOrder.php class PlaceOrder implements SplObserver { public function update(SplSubject $order) { echo 'We have ' . count($order) . ' orders now' . PHP_EOL; } } 30
  • 31. Running Interface Example <?php require_once 'Order.php'; require_once 'PlaceOrder.php'; $order = new Order(); $placeOrder = new PlaceOrder(); $order->attach($placeOrder); $order->notify(); $order->placeOrder(); $order->notify(); $order->placeOrder(); $order->notify(); $ php ./spl_observer.php We have 0 orders now We have 1 orders now We have 2 orders now 31
  • 32. SPL Exceptions • SPL Exceptions - templates - throw exceptions - common issues • Types of exceptions - LogicExceptions - RuntimeExceptions 32
  • 35. <?php Exceptions Example //file: spl_exception01.php class MyClass { public function giveANumberFromOneToTen($number) { if($number < 1 || $number > 10) { throw new OutOfBoundsException('Number should be between 1 and 10'); } echo $number . PHP_EOL; } } $my = new MyClass(); try { $my->giveANumberFromOneToTen(5); $my->giveANumberFromOneToTen(20); } catch (OutOfBoundsException $e) { echo $e->getMessage() . PHP_EOL; } Output: $ /usr/bin/php ./spl_exception01.php 5 Number should be between 1 and 10 35
  • 36. SplFunctions • functions for PHP and SPL in particular • often dealing with auto loading • some for internal referencing 36
  • 37. <?php SplFunctions Example interface foo {} interface bar {} class baz implements foo, bar {} class example extends baz {} var_dump(class_implements(new baz)); var_dump(class_implements(new example)); 37
  • 38. Output of SplFunctions array(2) { ["foo"]=> string(3) "foo" ["bar"]=> string(3) "bar" } array(2) { ["bar"]=> string(3) "bar" ["foo"]=> string(3) "foo" } 38
  • 39. SPLFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file. 39
  • 40. <?php SplFileInfo Example // use the current file to get information from $file = new SplFileInfo(dirname(__FILE__)); var_dump($file->isFile()); var_dump($file->getMTime()); var_dump($file->getSize()); var_dump($file->getFileInfo()); var_dump($file->getOwner()); //output bool(false) int(1244760945) int(408) object(SplFileInfo)#2 (0) { } int(501) 40
  • 41. Processing CSV with SPL Consider the following data.csv Derick Rethans;time Sebastian Bergmann;test Marcus Börger;iterate Ivo Jansch;enterprise Matthew Weier O'Phinney;extend Michelangelo van Dam;elephpant 41
  • 42. <?php SPL usage on CSV $info = new SplFileInfo('data.csv'); if ($info->isReadable()) { $file = $info->openFile(); $file->setFlags(SplFileObject::READ_CSV); $file->setCsvControl(';','"'); foreach ($file as $row) { list ($user, $term) = $row; if (null !== $user && null !== $term) { echo $user . ' is known for ' . $term . PHP_EOL; } } } //outputs Derick Rethans is known for time Sebastian Bergmann is known for test Marcus Börger is known for iterate Ivo Jansch is known for enterprise Matthew Weier O'Phinney is known for extend Michelangelo van Dam is known for elephpant 42
  • 43. Data Structures • Available in PHP 5.3.x - SplDoublyLinkedList ✓ SplStack ✓ SplQueue ✓ SplHeap ✓ SplMaxHeap ✓ SplMinHeap ✓ SplPriorityQueue 43
  • 44. Data Structures Example <?php // file: spl_stack01.php $stack = new SplStack(); $stack->push('Message 1'); $stack->push('Message 2'); $stack->push('Message 3'); echo $stack->pop() . PHP_EOL; echo $stack->pop() . PHP_EOL; echo $stack->pop() . PHP_EOL; Outputs: $ /usr/bin/php ./spl_stack01.php Message 3 Message 2 Message 1 44
  • 45. SplHeap • SplHeap is an abstract class - SplMinHeap implements SplHeap (low » high) - SplMaxHeap implements SplHeap (high » low) • stacking values w/o FIFO, FILO order 45
  • 46. Simple SplHeap example <?php $heap = new SplMinHeap; $heap->insert(5); $heap->insert(2); $heap->insert(8); $heap->insert(6); $heap->top(); while ($heap->valid()) { echo $heap->key() . ': ' . $heap->current() . PHP_EOL; $heap->next(); } //outputs 3: 2 2: 5 1: 6 0: 8 46
  • 47. JupilerLeague with SplHeap <?php class JupilerLeague extends SplHeap { public function compare($array1, $array2) { $values1 = array_values($array1); $values2 = array_values($array2); if ($values1[0] === $values2[0]) return 0; return $values1[0] < $values2[0] ? -1 : 1; } } $heap = new JupilerLeague(); $heap->insert(array ('AA Gent' => 15)); $heap->insert(array ('Anderlecht' => 20)); $heap->insert(array ('Cercle Brugge' => 11)); $heap->insert(array ('Charleroi' => 12)); $heap->insert(array ('Club Brugge' => 21)); $heap->insert(array ('G. Beerschot' => 15)); $heap->insert(array ('Kortrijk' => 10)); $heap->insert(array ('KV Mechelen' => 18)); $heap->insert(array ('Lokeren' => 10)); $heap->insert(array ('Moeskroen' => 7)); $heap->insert(array ('Racing Genk' => 11)); $heap->insert(array ('Roeselare' => 6)); $heap->insert(array ('Standard' => 20)); $heap->insert(array ('STVV' => 17)); $heap->insert(array ('Westerlo' => 10)); $heap->insert(array ('Zulte Waregem' => 15)); $heap->top(); while ($heap->valid()) { list ($team, $score) = each ($heap->current()); echo $team . ': ' . $score . PHP_EOL; $heap->next(); } 47
  • 48. JupilerLeague Scoreboard Club Brugge: 21 Anderlecht: 20 Standard: 20 KV Mechelen: 18 STVV: 17 Zulte Waregem: 15 AA Gent: 15 G. Beerschot: 15 Charleroi: 12 Racing Genk: 11 Cercle Brugge: 11 Kortrijk: 10 Lokeren: 10 Westerlo: 10 Moeskroen: 7 Roeselare: 6 48
  • 49. Conclusion SPL can help you solve common PHP issues it’s built-in, so why not use it it requires no “advanced skills” to use 49
  • 50. SPL is not all good • Matthew “Elazar” Turland pointed out: - Performance could be better (SPLStack) - ArrayObject doesn’t support all array functions • See his presentation: http://ishouldbecoding.com/publications 50
  • 51. Not a bridge too far... 51
  • 52. Only one minor thing... 52
  • 53. Documentation problem • php.net/spl needs more documentation ! • you can help on that part: - see http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows - you can set up phpdoc on each platform ! - Efnet: #php.doc channel - http://doc.php.net - phpdoc@php.net 53
  • 54. More about SPL... • main SPL documentation: http://php.net/spl • PHPro Tutorials on SPL: http://www.phpro.org/tutorials/Introduction-to-SPL.html • Lorenzo Alberton’s blog: http://www.alberton.info/php_5.3_spl_data_structures.html • http://www.colder.ch/news/01-08-2009/34/splobjectstorage-for- a-fa.html 54
  • 55. License This presentation is released under the Creative Commons Attribution-Share Alike 3.0 Unported License You are free: - to share : to copy, distribute and transmit the work - to remix : to adapt the work Under the following conditions: - attribution : You must attribute the work in the manner specified by the author or licensor - share alike : If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license See: http://creativecommons.org/licenses/by-sa/3.0/ 55
  • 56. Photo credits Marcus Boerger - Sebastian Bergmann http://www.flickr.com/photos/sebastian_bergmann/173891817 Golden Gate Bridge - Felix De Vliegher http://www.flickr.com/photos/felixdv/2883471022/ 56
  • 57. Thank you Find my slides on http://slideshare.net/DragonBe Don’t forget to vote ! http://joind.in/613 57

Hinweis der Redaktion

  1. Promotion of SPL
  2. now everyone uses PHP 5.3 for new projects, right ?
  3. Created by Marcus Boerger
  4. Created by Marcus Boerger
  5. Definition of SPL
  6. SPL functions - base class functions - autoloading functions
  7. - looking at phpinfo() for PHP 5.3 - 6 interfaces - 39 classes
  8. easiest way converting array into an object
  9. blue code is a lambda or anonymous function used in PHP 5.3
  10. More info found on http://php.net/spl
  11. arrays are stored as a whole in memory iterators just the element that has focus
  12. interface: design by contract for classes
  13. example using SplObserver, SplSubject and Countable interface
  14. definition from php.net
  15. SplInfo provides a lot of details about a given file
  16. work with lots of CSV data files
  17. using a data model, makes things a whole lot easier
  18. let&amp;#x2019;s start of simple, using the SplStack here
  19. simple example processing test results
  20. modify the compare method to use the values of a given array Soccer != football, but you get the point here, right ?
  21. so, once you know how, it&amp;#x2019;s not a bridge too far