$30 off During Our Annual Pro Sale. View Details »

(Re)discovering the SPL - IPC14

Joshua Thijssen
June 02, 2014
56

(Re)discovering the SPL - IPC14

Joshua Thijssen

June 02, 2014
Tweet

Transcript

  1. 1
    Joshua Thijssen
    jaytaph
    (Re)discovering the SPL

    View Slide

  2. 2
    Q: Have you ever used the SPL?

    View Slide

  3. 3
    Q: Have you ever used the SPL
    without going nuts?

    View Slide

  4. 4
    SPL Documentation

    View Slide

  5. 4
    SPL Documentation
    http://www.php.net/spl

    View Slide

  6. 5

    View Slide

  7. 5

    View Slide

  8. 6

    View Slide

  9. 7

    View Slide

  10. ➡ Not enough documentation.
    7

    View Slide

  11. ➡ Not enough documentation.
    ➡ Very few examples.
    7

    View Slide

  12. ➡ Not enough documentation.
    ➡ Very few examples.
    ➡ Wrong / missing in some cases.
    7

    View Slide

  13. ➡ Interfaces
    ➡ Iterators
    ➡ Data structures
    ➡ Exceptions
    ➡ Miscellaneous functionality
    8
    Every SPL talk available:

    View Slide

  14. 9
    Don’t get scared!
    The SPL is awesomesauce!

    View Slide

  15. 10
    INTERFACES

    View Slide

  16. 11
    Traversable
    (not an “spl interface”)

    View Slide

  17. 12

    View Slide

  18. ➡ Traversable cannot be implemented.
    12

    View Slide

  19. ➡ Traversable cannot be implemented.
    ➡ Traversable can be detected (instanceof).
    12

    View Slide

  20. ➡ Traversable cannot be implemented.
    ➡ Traversable can be detected (instanceof).
    ➡ foreach() detects traversable interfaces
    and does magic.
    12

    View Slide

  21. 13
    Iterator
    (still not an “spl interface”)

    View Slide

  22. Userland interface to make
    an object traversable
    14

    View Slide

  23. Iterator extends Traversable {
    /* Methods */
    abstract public mixed current ( void )
    abstract public scalar key ( void )
    abstract public void next ( void )
    abstract public void rewind ( void )
    abstract public boolean valid ( void )
    }
    15
    Iterator interface:

    View Slide

  24. ➡ Iterator
    ➡ FilterIterators
    ➡ “Chains” iterators together
    ➡ IteratorAggregate
    16

    View Slide

  25. $dir = opendir(".");
    while (($file = readdir($dir)) !== false) {
    # Business logic happens here
    print "file: $file\n";
    }
    Let’s read directory content
    17

    View Slide

  26. $dir = opendir(".");
    while (($file = readdir($dir)) !== false) {
    # hack: only display mp3 files
    if (! preg_match('|\.mp3$|i', $file)) {
    continue;
    }
    # Business logic happens here
    print "file: $file\n";
    }
    18
    Filter on *.mp3

    View Slide

  27. ➡ Filter all MP3 and all JPG files.
    19

    View Slide

  28. ➡ Filter all MP3 and all JPG files.
    ➡ Filter all MP3 files that are larger than
    6MB.
    19

    View Slide

  29. ➡ Filter all MP3 and all JPG files.
    ➡ Filter all MP3 files that are larger than
    6MB.
    ➡ Do not filter at all.
    19

    View Slide

  30. ➡ Filter all MP3 and all JPG files.
    ➡ Filter all MP3 files that are larger than
    6MB.
    ➡ Do not filter at all.
    ➡ Search sub-directories as well.
    19

    View Slide

  31. ➡ Filter all MP3 and all JPG files.
    ➡ Filter all MP3 files that are larger than
    6MB.
    ➡ Do not filter at all.
    ➡ Search sub-directories as well.
    ➡ Search multiple directories.
    19

    View Slide

  32. 20

    View Slide

  33. ➡ How to test? (we can’t)
    20

    View Slide

  34. ➡ How to test? (we can’t)
    ➡ How to maintain? (we can’t)
    20

    View Slide

  35. ➡ How to test? (we can’t)
    ➡ How to maintain? (we can’t)
    ➡ How to reuse? (we can’t)
    20

    View Slide

  36. $it = new DirectoryIterator(".");
    foreach ($it as $fi) {
    print "File: ".$fi->getPathname()."\n";
    }
    21
    Directory Iterator

    View Slide

  37. $it = new DirectoryIterator(".");
    $it2 = new RegexIterator($it, "/\.mp3$/i");
    foreach ($it2 as $fi) {
    print "File: ".$fi->getPathname()."\n";
    }
    22
    Directory Iterator + filtering

    View Slide

  38. $it = new DirectoryIterator(".");
    $it2 = new RegexIterator($it, "/\.mp3$/i");
    $it3 = new FilesizeIterator($it2, 0, 6 * 1024 * 1024);
    $it4 = new LimitIterator($it3, 10, 5);
    foreach ($it4 as $fi) {
    print "File: ".$fi->getPathname()."\n";
    }
    23
    Iterator chaining

    View Slide

  39. $it = new DirectoryIterator(".");
    $it2 = new RegexIterator($it, "/\.mp3$/i");
    $it3 = new FilesizeIterator($it2, 0, 6 * 1024 * 1024);
    $it4 = new LimitIterator($it3, 10, 5);
    foreach ($it4 as $fi) {
    print "File: ".$fi->getPathname()."\n";
    }
    23
    Iterator chaining

    View Slide

  40. 24

    View Slide

  41. ✓ Reusable
    We can use iterators where ever we want.
    24

    View Slide

  42. ✓ Reusable
    We can use iterators where ever we want.
    ✓ Testable
    Iterators can be tested separately.
    24

    View Slide

  43. ✓ Reusable
    We can use iterators where ever we want.
    ✓ Testable
    Iterators can be tested separately.
    ✓ Maintainable
    No need to adapt our business logic.
    24

    View Slide

  44. 25
    Countable
    (hurrah! An “spl interface”!)

    View Slide

  45. class myIterator implements \Iterator {
    ...
    }
    $a = array(1, 2, 3);
    $it = new myIterator($a);
    print count($it);
    26

    View Slide

  46. class myIterator implements \Iterator {
    ...
    }
    $a = array(1, 2, 3);
    $it = new myIterator($a);
    print count($it);
    1
    26

    View Slide

  47. class myCountableIterator extends myIterator implements Countable
    {
    function count() {
    return count($this->_elements);
    }
    }
    $a = array(1, 2, 3, 4, 5);
    $it = new myCountableIterator($a);
    print count($it);
    27

    View Slide

  48. class myCountableIterator extends myIterator implements Countable
    {
    function count() {
    return count($this->_elements);
    }
    }
    $a = array(1, 2, 3, 4, 5);
    $it = new myCountableIterator($a);
    print count($it);
    5
    27

    View Slide

  49. class myCountableIterator extends myIterator implements Countable
    {
    function count() {
    return count($this->_arr);
    }
    }
    $a = array(1, 2, 3, 4, 5);
    $it = new myCountableIterator($a);
    $it2 = new limitIterator($it, 0, 3);
    print count($it2);
    28

    View Slide

  50. class myCountableIterator extends myIterator implements Countable
    {
    function count() {
    return count($this->_arr);
    }
    }
    $a = array(1, 2, 3, 4, 5);
    $it = new myCountableIterator($a);
    $it2 = new limitIterator($it, 0, 3);
    print count($it2);
    1
    28

    View Slide

  51. 29
    SeekableIterator

    View Slide

  52. 30

    View Slide

  53. ➡ It’s not an iterator, it’s an interface.
    30

    View Slide

  54. ➡ It’s not an iterator, it’s an interface.
    ➡ seek()
    30

    View Slide

  55. ➡ It’s not an iterator, it’s an interface.
    ➡ seek()
    ➡ Implementing “seekableIterator” can speed up
    other iterators.
    30

    View Slide

  56. ➡ It’s not an iterator, it’s an interface.
    ➡ seek()
    ➡ Implementing “seekableIterator” can speed up
    other iterators.
    ➡ LimitIterator makes use of “seekableIterator”
    30

    View Slide

  57. 31
    ITERATORS

    View Slide

  58. SPL Iterators
    ➡ AppendIterator
    ➡ ArrayIterator
    ➡ CachingIterator
    ➡ CallbackFilterIterator
    ➡ DirectoryIterator
    ➡ EmptyIterator
    ➡ FilesystemIterator
    ➡ FilterIterator
    ➡ GlobIterator
    ➡ InfiniteIterator
    ➡ IteratorIterator
    ➡ LimitIterator
    ➡ MultipleIterator
    ➡ NoRewindIterator
    ➡ ParentIterator
    ➡ RecursiveArrayIterator
    ➡ RecursiveCachingIterator
    ➡ RecursiveCallbackFilterIterator
    ➡ RecursiveDirectoryIterator
    ➡ RecursiveFilterIterator
    ➡ RecursiveIteratorIterator
    ➡ RecursiveRegexIterator
    ➡ RecursiveTreeIterator
    ➡ RegexIterator
    ➡ SimpleXMLIterator
    32

    View Slide

  59. SPL Iterators
    33
    http://lxr.php.net/xref/PHP_5_5/ext/spl/internal/

    View Slide

  60. 34

    View Slide

  61. ➡ IteratorIterator?
    35

    View Slide

  62. ➡ IteratorIterator?
    ➡ RecursiveIterator?
    35

    View Slide

  63. ➡ IteratorIterator?
    ➡ RecursiveIterator?
    ➡ RecursiveIteratorIterator?
    35

    View Slide

  64. ➡ IteratorIterator?
    ➡ RecursiveIterator?
    ➡ RecursiveIteratorIterator?
    ➡ RecursiveCallbackFilterIterator?
    35

    View Slide

  65. ➡ IteratorIterator?
    ➡ RecursiveIterator?
    ➡ RecursiveIteratorIterator?
    ➡ RecursiveCallbackFilterIterator?
    35

    View Slide

  66. 36
    IteratorIterator

    View Slide

  67. 37
    Turns traversable “things” into an iterator

    View Slide

  68. 38
    $it = new myIterator();
    if ($it instanceof \IteratorAggregate) {
    $it = $it->getIterator();
    }
    $it2 = new \LimitIterator($it, 5, 10);

    View Slide

  69. 39
    $it = new myIterator();
    $it2 = new \IteratorIterator($it);
    $it3 = new \LimitIterator($it2, 5, 10);

    View Slide

  70. 40
    Recursive*Iterator

    View Slide

  71. 41
    $it = new ArrayIterator(
    array(“foo”, “bar”, array(“qux”, “wox”), “baz”));
    foreach ($it as $v) {
    print $v . “\n”;
    }

    View Slide

  72. 41
    $it = new ArrayIterator(
    array(“foo”, “bar”, array(“qux”, “wox”), “baz”));
    foreach ($it as $v) {
    print $v . “\n”;
    }
    foo
    bar
    Array
    baz

    View Slide

  73. 42
    $it = new RecursiveArrayIterator(
    array(“foo”, “bar”, array(“qux”, “wox”), “baz”));
    foreach ($it as $v) {
    print $v . “\n”;
    }

    View Slide

  74. 42
    $it = new RecursiveArrayIterator(
    array(“foo”, “bar”, array(“qux”, “wox”), “baz”));
    foreach ($it as $v) {
    print $v . “\n”;
    }
    foo
    bar
    Array
    baz

    View Slide

  75. 43
    $it = new RecursiveArrayIterator(
    array(“foo”, “bar”, array(“qux”, “wox”), “baz”));
    $it2 = new RecursiveIteratorIterator($it);
    foreach ($it2 as $v) {
    print $v . “\n”;
    }

    View Slide

  76. 43
    $it = new RecursiveArrayIterator(
    array(“foo”, “bar”, array(“qux”, “wox”), “baz”));
    $it2 = new RecursiveIteratorIterator($it);
    foreach ($it2 as $v) {
    print $v . “\n”;
    }
    foo
    bar
    qux
    wox
    baz

    View Slide

  77. 44
    “Recursive” iterators add the POSSIBILITY
    to recursively iterate over data.
    You still need to implement it!

    View Slide

  78. 45
    RecursiveCallbackFilterIterator

    View Slide

  79. ➡ Enables recursivity
    ➡ Is a filter iterator (does not necessarily
    return all the elements)
    ➡ Filters through a callback function.
    46

    View Slide

  80. 47
    CachingIterator

    View Slide

  81. 48
    2 for the price of 1

    View Slide

  82. 49
    ➡ Lookahead iterator
    ➡ Caches values, but not really :(
    ➡ Powerful __tostring() functionality (which
    probably no one uses)

    View Slide

  83. 50
    $alphaIterator = new ArrayIterator(range("A", "Z"));
    $it = new CachingIterator($alphaIterator);
    foreach ($it as $v) {
    if (! $it->hasNext()) {
    print "last letter: ";
    }
    print $v . "\n";
    }
    // A
    // ...
    // Y
    // last letter: Z

    View Slide

  84. 51
    $alphaIterator = new ArrayIterator(range("A", "Z"));
    $it = new CachingIterator($alphaIterator);
    foreach ($it as $v) {
    if (! $it->hasNext()) {
    print "last letter: ";
    }
    print $v . "\n";
    }
    print "The fourth letter of the alphabet is: ".$it[3]."\n";

    View Slide

  85. 52

    View Slide

  86. ➡ Don’t change cached data (you could, but
    don’t)
    52

    View Slide

  87. ➡ Don’t change cached data (you could, but
    don’t)
    ➡ It doesn’t use the cached data on
    consecutive calls to the iterator.
    52

    View Slide

  88. ➡ Don’t change cached data (you could, but
    don’t)
    ➡ It doesn’t use the cached data on
    consecutive calls to the iterator.
    ➡ It clears the cache on a rewind() (and
    thus, a next foreach() loop)
    52

    View Slide

  89. 53
    SPL Iterators,..

    View Slide

  90. ➡ It has “quirks” that are easily solvable (but
    breaks BC)
    ➡ Documentation is not always up to date.
    ➡ Naming is VERY confusing (caching
    iterator, recursiveIterator, seekableIterator)
    ➡ But the iterators are worth it!
    54

    View Slide

  91. 55
    DATA STRUCTURES

    View Slide

  92. 56
    ➡ SplDoublyLinkedList
    ➡ SplStack
    ➡ SplQueue
    ➡ SplHeap
    ➡ SplMinHeap
    ➡ SplMaxHeap
    ➡ SplPriorityQueue
    ➡ SplFixedArray
    ➡ SplObjectStorage

    View Slide

  93. 57
    ➡ Every data structure has its strength and
    weaknesses.
    ➡ Big-Oh O(1), O(n), O(log n) etc...
    ➡ Balance between time (CPU) and space
    (memory)
    ➡ PHP arrays are quite good!
    ➡ But sometimes other data structures are
    better.

    View Slide

  94. 58
    PubQuiz

    View Slide

  95. 59
    http://www.flickr.com/photos/sostark/26697448/

    View Slide

  96. 59
    http://www.flickr.com/photos/sostark/26697448/
    SplStack

    View Slide

  97. 60

    View Slide

  98. 60
    SplQueue

    View Slide

  99. 61
    http://www.flickr.com/photos/blogchef/4764248456/sizes/z/in/photostream/

    View Slide

  100. 61
    http://www.flickr.com/photos/blogchef/4764248456/sizes/z/in/photostream/
    Spl(Doubly)LinkedList

    View Slide

  101. 62
    http://www.flickr.com/photos/pediddle/54326823/

    View Slide

  102. 62
    http://www.flickr.com/photos/pediddle/54326823/
    SplObjectStorage

    View Slide

  103. 63

    View Slide

  104. 63
    SplPriorityQueue

    View Slide

  105. 64
    http://farm4.static.flickr.com/3224/3072017987_ee454536da.jpg

    View Slide

  106. 64
    http://farm4.static.flickr.com/3224/3072017987_ee454536da.jpg
    SplHeap

    View Slide

  107. 65
    http://en.wikipedia.org/wiki/File:USA.NM.VeryLargeArray.02.jpg

    View Slide

  108. 65
    http://en.wikipedia.org/wiki/File:USA.NM.VeryLargeArray.02.jpg
    SplFixedArray

    View Slide

  109. 66
    ➡ Use wisely:
    ➡ Don’t use SplStack / SplQueue for
    random reads.
    ➡ Don’t use FixedArrays when you need
    speed boosts.

    View Slide

  110. 67
    EXCEPTIONS

    View Slide

  111. ➡ BadFunctionCallException
    ➡ BadMethodCallException
    ➡ DomainException
    ➡ InvalidArgumentException
    ➡ LengthException
    ➡ LogicException
    ➡ OutOfBoundsException
    ➡ OutOfRangeException
    ➡ OverflowException
    ➡ RangeException
    ➡ RuntimeException
    ➡ UnderflowException
    ➡ UnexpectedValueException
    68

    View Slide

  112. ➡ BadFunctionCallException
    ➡ BadMethodCallException
    ➡ DomainException
    ➡ InvalidArgumentException
    ➡ LengthException
    ➡ OutOfRangeException
    69
    Logic Exceptions Runtime Exceptions
    ➡ OutOfBoundsException
    ➡ OverflowException
    ➡ RangeException
    ➡ UnderflowException
    ➡ UnexpectedValueException

    View Slide

  113. 70
    function foo($str) {
    if ($str == “The Spanish Inquisition”) {
    throw new \UnexpectedValueException(“Nobody expects ”.$str);
    }
    ...
    }

    View Slide

  114. 71
    function foo($str) {
    if ($str == “The Spanish Inquisition”) {
    throw new \InvalidArgumentException(“Nobody expects ”.$str);
    }
    ...
    }
    Logic, not runtime

    View Slide

  115. 72
    function foo($str, $int) {
    if (! is_string($str)) {
    throw new \InvalidArgumentException(“Invalid type”);
    }
    if ($int < 0 || $int > 10) {
    throw new \OutOfRangeException(“should be between 0 and 10);
    }
    ...
    }

    View Slide

  116. 73
    Never throw “\Exception”
    Always catch “\Exception”

    View Slide

  117. 74
    MISC

    View Slide

  118. ➡ SPL Autoloading
    ➡ SplFileInfo class
    ➡ Spl(Temp)FileObject
    ➡ ArrayObject
    ➡ SplObserver / SplSubject
    75

    View Slide

  119. 76
    Autoloader

    View Slide

  120. 77
    spl_autoload_register(“spl_autoload_call”);

    View Slide

  121. 77
    spl_autoload_register(“spl_autoload_call”);
    Throws logicException

    View Slide

  122. 78
    spl_autoload_unregister(“spl_autoload_call”);

    View Slide

  123. 78
    spl_autoload_unregister(“spl_autoload_call”);
    - Removes ALL the autoloaders!
    - Destroys the autoload stack.
    - Set your house on fire.

    View Slide

  124. 79
    ArrayObject

    View Slide

  125. 80
    ArrayObjects are not objects that acts like arrays
    ArrayObjects are objects that acts like arrays

    View Slide

  126. 81
    $a = array("foo", "bar");
    $b = $a;
    $b[] = "baz";
    print_r ($a);
    print_r ($b);

    View Slide

  127. 81
    $a = array("foo", "bar");
    $b = $a;
    $b[] = "baz";
    print_r ($a);
    print_r ($b);
    Array
    (
    [0] => foo
    [1] => bar
    )
    Array
    (
    [0] => foo
    [1] => bar
    [2] => baz
    )

    View Slide

  128. 82
    $a = new ArrayObject();
    $a[] = "foo";
    $a[] = "bar";
    $b = $a;
    $b[] = "baz";
    print_r (iterator_to_array($a));
    print_r (iterator_to_array($b));

    View Slide

  129. 82
    $a = new ArrayObject();
    $a[] = "foo";
    $a[] = "bar";
    $b = $a;
    $b[] = "baz";
    print_r (iterator_to_array($a));
    print_r (iterator_to_array($b));
    Array
    (
    [0] => foo
    [1] => bar
    [2] => baz
    )
    Array
    (
    [0] => foo
    [1] => bar
    [2] => baz
    )

    View Slide

  130. 83
    How can we make using the SPL easier?
    http://noscope.com/photostream/albums/various/I_Want_To_Believe_01.jpg

    View Slide

  131. 84
    ➡ The (first and only)
    book about the SPL.
    ➡ Written by me (so you
    know it’s good :P)
    ➡ Fixes the
    documentation problem
    of the SPL (or a wobbly
    table)

    View Slide

  132. 85

    View Slide

  133. ➡ Adaption of the SPL will only happen
    when developers can become familiar
    with it.
    85

    View Slide

  134. ➡ Adaption of the SPL will only happen
    when developers can become familiar
    with it.
    ➡ There is currently no real way to
    familiarize yourself with the SPL.
    85

    View Slide

  135. ➡ BLOG!
    ➡ Update documentation.
    ➡ Find the quirks (and maybe even solve
    them).
    86

    View Slide

  136. http://farm1.static.flickr.com/73/163450213_18478d3aa6_d.jpg 87

    View Slide

  137. 88
    Find me on twitter: @jaytaph
    Find me for development and training: www.noxlogic.nl
    Find me on email: [email protected]
    Find me for blogs: www.adayinthelifeof.nl

    View Slide