Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dark Corners Of The SPL - Dallas PHP July

Dark Corners Of The SPL - Dallas PHP July

PHP is a huge language, with lots of "kitchen sink" functionality for you to build data structures with. But did you know PHP includes a standard library that has built-in structures like linked lists, queues, stacks, and higher-performance arrays? This talk will cover a few of the more interesting ones in depth, including how to use them and most importantly why you'd use them over other solutions.

Omni Adams

July 10, 2018
Tweet

More Decks by Omni Adams

Other Decks in Programming

Transcript

  1. Dark Corners of the SPL Photo By: SSG Ryan Boas

  2. http://foto.wuestenigel.com/work-in-progress/

  3. Photo By: Tom Walsh

  4. Photo By: NASA Goddard Space Flight Center

  5. Data Structures SplDoublyLinkedList SplStack SplQueue SplHeap SplMaxHeap SplMinHeap SplPriorityQueue SplFixedArray

    SplObjectStorage
  6. Big-O O(1) - Constant no matter how big your data

    O(log n) - Grows slowly with your data O(n) - Grows linearly with your data O(n2) - Grows quadratically with your data O(2n) - Grows exponentially with your data O(n!) - LOL
  7. Photo By: Cmglee

  8. Data Structures SplDoublyLinkedList SplStack SplQueue SplPriorityQueue SplFixedArray SplObjectStorage SplHeap SplMaxHeap

    SplMinHeap
  9. Image By: Bhavikp19

  10. SplDoublyLinkedList Access: O(n) Search: O(n) Insert: O(1) Delete: O(1)

  11. $list = new SplDoublyLinkedList(); $list->push('Ash'); $list->push('Bingo'); $list->push('Charlie'); $list->push('Echo'); foreach ($list

    as $key => $character) { echo $key, ' ', $character, PHP_EOL; } echo PHP_EOL;
  12. $list->push('Echo'); foreach ($list as $key => $character) { echo $key,

    ' ', $character, PHP_EOL; } echo PHP_EOL; 0 Ash 1 Bingo 2 Charlie 3 Echo
  13. 0 Ash 1 Bingo 2 Charlie 3 Echo $list->add(3, 'Delta');

    foreach ($list as $key => $character) { echo $key, ' ', $character, PHP_EOL; } echo PHP_EOL;
  14. foreach ($list as $key => $character) { echo $key, '

    ', $character, PHP_EOL; } echo PHP_EOL; 0 Ash 1 Bingo 2 Charlie 3 Delta 4 Echo
  15. 2 Charlie 3 Delta 4 Echo $list->rewind(); echo sprintf( "It's

    %s's turn! They take out %s!", $list->current(), $list[2] ), PHP_EOL; $list->offsetUnset(2); foreach ($list as $key => $character) { echo $key, ' ', $character, PHP_EOL; } echo PHP_EOL;
  16. foreach ($list as $key => $character) { echo $key, '

    ', $character, PHP_EOL; } echo PHP_EOL; It's Ash's turn! They take out Charlie! 0 Ash 1 Bingo 2 Delta 3 Echo
  17. Data Structures SplDoublyLinkedList SplStack SplQueue SplPriorityQueue SplFixedArray SplObjectStorage SplHeap SplMaxHeap

    SplMinHeap
  18. Data Structures SplDoublyLinkedList SplStack SplQueue SplPriorityQueue SplFixedArray SplObjectStorage SplHeap SplMaxHeap

    SplMinHeap
  19. Image By: Ermishin

  20. SplHeap Access: O(log n) Search: O(log n) Insert: O(log n)

    Delete: O(log n)
  21. class Character { public $name; public $initiative; public function __construct(

    string $name, int $init ) { $this->name = $name; $this->initiative = $init; } }
  22. class InitiativeHeap extends SplMaxHeap { public function compare($a, $b) :

    int { return $a->initiative - $b->initiative; } }
  23. $init = new InitiativeHeap(); $init->insert(new Character('Ash', random_int(1, 30))); $init->insert(new Character('Bingo',

    random_int(1, 30))); $init->insert(new Character('Charlie', random_int(1, 30))); $init->insert(new Character('Echo', random_int(1, 30))); foreach ($init as $character) { echo sprintf( '%s %d', $character->name, $character->initiative ), PHP_EOL; }
  24. foreach ($init as $character) { echo sprintf( '%s %d', $character->name,

    $character->initiative ), PHP_EOL; } Ash 30 Echo 27 Charlie 16 Bingo 12
  25. Data Structures SplDoublyLinkedList SplStack SplQueue SplHeap SplMaxHeap SplMinHeap SplPriorityQueue SplFixedArray

    SplObjectStorage
  26. SplFixedArray Access: O(1) Search: O(n) Insert: O(1) or O(n) Delete:

    O(n)
  27. $a = [ 'foo' => 1, 'bar' => new StdClass(),

    1 => null, false => true, ]; var_dump($a); array(3) { [“foo"]=>int(1) [“bar"]=>object(stdClass)#1 (0) {} [1]=>NULL [0]=>bool(true) } $b = [ 0 => 10, 2 => 22, PHP_INT_MAX => PHP_INT_MAX, ]; var_dump($b); array(3) { [0]=>int(10) [2]=>int(22) [9223372036854775807]=> int(9223372036854775807) }
  28. $c = new SplFixedArray(3); $c[0] = 10; $c[1] = 20;

    $c[2] = 42; var_dump($c); object(SplFixedArray)#2 (3) { [0]=>int(10) [1]=>int(20) [2]=>int(42) }
  29. $d = new SplFixedArray(3); $d[0] = 10; $d[1] = 20;

    $d[2] = 42; $d[3] = 99; var_dump($d); PHP Fatal error: Uncaught RuntimeException: Index invalid or out of range in SplFixedArray.php:33 Stack trace: #0 {main} thrown in SplFixedArray.php on line 33
  30. $e = new SplFixedArray(3); $e[0] = 10; $e[1] = 20;

    $e[2] = 42; $e->setSize(4); $e[3] = 99; var_dump($e); object(SplFixedArray)#5 (4) { [0]=>int(10) [1]=>int(20) [2]=>int(42) [3]=>int(99) }
  31. Data Structures SplDoublyLinkedList SplStack SplQueue SplHeap SplMaxHeap SplMinHeap SplPriorityQueue SplFixedArray

    SplObjectStorage
  32. Data Structures SplDoublyLinkedList SplStack SplQueue SplHeap SplMaxHeap SplMinHeap SplPriorityQueue SplFixedArray

    SplObjectStorage
  33. $char = new Character(); $storage = []; $storage[$char] = new

    Character(); Warning: Illegal offset type in SplObjectStorage.php on line 3
  34. class Character {} class Enemy {} class Buff {} class

    Ailment {}
  35. class Buff {} class Ailment {} $fighter = new Character();

    $mage = new Character(); $goblin = new Enemy(); $ogre = new Enemy(); $haste = new Buff(); $poison = new Ailment();
  36. $encounter = new SplObjectStorage(); $encounter->attach($fighter, [$haste]); $encounter->attach($mage, [$haste, $poison]); $encounter[$goblin]

    = [$poison]; $encounter->attach($ogre); foreach ($encounter as $key => $value) { print_r($key); echo PHP_EOL; print_r($value); } $poison = new Ailment();
  37. foreach ($encounter as $key => $value) { print_r($key); echo PHP_EOL;

    print_r($value); } 0 Character Object() 1 Character Object() 2 Enemy Object() 3 Enemy Object()
  38. object(SplObjectStorage)#7 (1) { ["storage":"SplObjectStorage":private]=>array(4) { ["000000005460553e00000000545bda63"]=>array(2) { ["obj"]=>object(Character)#1 (0) {}

    ["inf"]=>array(1) { [0]=>object(Buff)#5 (0) {} } } } }
  39. foreach ($encounter as $key) { print_r($key); print_r($encounter[$key]); }

  40. print_r($encounter[$key]); } Character Object() // fighter Array( [0] => Buff

    Object() // haste ) Character Object() // mage Array( [0] => Buff Object() // haste [1] => Ailment Object() // poison ) Enemy Object() // golbin Array( [0] => Ailment Object() // poison ) Enemy Object()
  41. –Lady Gaga “I just am committed wholeheartedly to theatre with

    no intermission.”
  42. SPL Interfaces Countable OuterIterator RecursiveIterator SeekableIterator SplObserver SplSubject

  43. Countable interface Countable { public function count() : int; }

  44. class Countable { public function count() : int { return

    42; } } $foo = new Countable(); echo count($foo), PHP_EOL;
  45. class Countable { public function count() : int { return

    42; } } $foo = new Countable(); echo count($foo), PHP_EOL; Warning: count(): Parameter must be an array or an object that implements Countable in Countable.php on line 13 1
  46. object that implements Countable in Countable.php on line 13 1

    class ActuallyCountable implements Countable { public function count() { return 42; } } $bar = new ActuallyCountable(); echo count($bar), PHP_EOL;
  47. SplSubject interface SplSubject { function attach(SplObserver $observer): void; function detach(SplObserver

    $observer): void; function notify(): void; }
  48. SplObserver interface SplObserver { function update(SplSubject $subject): void; }

  49. https://speakerdeck.com/omnicolor/dark-corners-of-the-spl https://joind.in/ https://twitter.com/omnicolor Omni Adams omni@digitaldarkness.com