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

Dark Corners - Tek

Dark Corners - Tek

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

April 23, 2024
Tweet

More Decks by Omni Adams

Other Decks in Technology

Transcript

  1. 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 https://en.wikipedia.org/wiki/Big_O
  2. $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; https://github.com/omnicolor/dark-corners-of-the-spl/blob/master/SplDoublyLinkedList.php
  3. 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;
  4. foreach ($list as $key => $character) { } echo PHP_EOL;

    0 Ash 1 Bingo 2 Charlie 3 Delta 4 Echo
  5. 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;
  6. foreach ($list as $key => $character) { } echo PHP_EOL;

    It's Ash's turn! They take out Charlie! 0 Ash 1 Bingo 2 Delta 3 Echo
  7. SplStack performance $startTime = microtime(true); $startSize = memory_get_usage(); $arrayStack =

    []; for ($i = 0; $i < $numItems; $i++) { array_push($arrayStack, mt_rand()); } $peakSize = memory_get_usage(); while (!empty($arrayStack)) { echo array_pop($arrayStack), ' '; } echo PHP_EOL; $startTime = microtime(true); $startSize = memory_get_usage(); $stack = new SplStack(); for ($i = 0; $i < $numItems; $i++) { $stack->push(mt_rand()); } $peakSize = memory_get_usage(); while (!$stack->isEmpty()) { echo $stack->pop(), ' '; } echo PHP_EOL; https://github.com/omnicolor/dark-corners-of-the-spl/blob/master/SplStackTest.php
  8. class Character { public $name; public $initiative; public function __construct(

    string $name, int $init ) { $this->name = $name; $this->initiative = $init; } } https://github.com/omnicolor/dark-corners-of-the-spl/blob/master/SplHeap.php
  9. class InitiativeHeap extends SplMaxHeap { public function compare($a, $b) :

    int { return $a->initiative - $b->initiative; } }
  10. $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; }
  11. foreach ($init as $character) { echo sprintf( '%s %d', $character->name,

    $character->initiative ), PHP_EOL; } Ash 30 Echo 27 Charlie 16 Bingo 12
  12. $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) } https://github.com/omnicolor/dark-corners-of-the-spl/blob/master/SplFixedArray.php
  13. $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) }
  14. $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
  15. $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) }
  16. $char = new Character(); $storage = []; $storage[$char] = [‘alive’

    => true]; Warning: Illegal offset type in SplObjectStorage.php on line 3
  17. class Buff {} class Ailment {} $fighter = new Character();

    $mage = new Character(); $goblin = new Enemy(); $ogre = new Enemy(); $haste = new Buff(); $poison = new Ailment();
  18. $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();
  19. 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()
  20. 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() // goblin Array( [0] => Ailment Object() // poison ) Enemy Object()
  21. class Countable { public function count() : int { return

    42; } } $foo = new Countable(); echo count($foo), PHP_EOL;
  22. 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
  23. 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;
  24. // ... public function attach(\SplObserver $observer): void { $this->observers->attach($observer); }

    class Character implements \SplSubject, \SplObserver { public $name; protected $observers; protected $message; public function __construct(string $name) { $this->name = $name; $this->observers = new \SplObjectStorage(); } // ...
  25. // ... public function update(\SplSubject $subject): void { // ...

    public function attach(\SplObserver $observer): void { $this->observers->attach($observer); } public function detach(\SplObserver $observer): void { $this->observers->detach($observer); } public function notify(): void { foreach ($this->observers as $observer) { $observer->update($this); } } // ... } // ...
  26. // ... public function sendMessage(string $message): void { $this->message =

    $message; $this->notify(); foreach ($this->observers as $observer) { $observer->update($this); } } // ... // ... public function update(\SplSubject $subject): void { echo sprintf( '%s received message: %s', $this->name, $subject->readMessage() ), PHP_EOL; } // ...
  27. $subject->readMessage() ), PHP_EOL; } // ... } $fighter = new

    Character('Fighter'); $mage = new Character('Mage'); $thief = new Character('Thief'); // ... public function sendMessage(string $message): void { $this->message = $message; $this->notify(); } public function readMessage(): string { return sprintf( '%s said "%s"', $this->name, $this->message ); } }
  28. return sprintf( '%s said "%s"', $this->name, $this->message ); } }

    } $fighter = new Character('Fighter'); $mage = new Character('Mage'); $thief = new Character('Thief'); $paladin = new Character('Paladin'); // Fighter, mage, and paladin listen to everyone else. $mage->attach($fighter); $thief->attach($fighter); $paladin->attach($fighter); $fighter->attach($mage); $thief->attach($mage); $paladin->attach($mage); $fighter->attach($paladin); $mage->attach($paladin); $thief->attach($paladin); $thief->attach($paladin);
  29. // Thief doesn't pay attention to anyone else, jerk. $paladin->sendMessage(

    ’Get ready for a fight. Mage, can you buff us?’ ); $mage->sendMessage('Sure, give me a second.'); $fighter->sendMessage('LEEEEROY JENKINS!'); $thief->sendMessage('Dangit Fighter!'); $thief->attach($mage); $paladin->attach($mage); $fighter->attach($paladin); $mage->attach($paladin);
  30. Fighter received message: Paladin said "Get ready for a fight.

    Mage, can you buff us?" Mage received message: Paladin said "Get ready for a fight. Mage, can you buff us?" Fighter received message: Mage said "Sure, give me a second." Paladin received message: Mage said "Sure, give me a second." Mage received message: Fighter said "LEEEEROY JENKINS!" Paladin received message: Fighter said "LEEEEROY JENKINS!" Fighter received message: Thief said "Dangit Fighter!" Mage received message: Thief said "Dangit Fighter!" Paladin received message: Thief said "Dangit Fighter!"
  31. SPL Exceptions BadFunctionCallException BadMethodCallException DomainException InvalidArgumentException LengthException LogicException OutOfBoundsException OutOfRangeException

    Over fl owException RangeException RuntimeException Under fl owException UnexpectedValueException https://secure.php.net/manual/en/spl.exceptions.php
  32. class SplFileInfo implements Stringable { public __construct(string $ fi lename);

    public getATime(): int|false; public getBasename(string $suf fi x = ''): string; public getCTime(): int|false; public getExtension(): string; public getFileInfo(?string $class = null): SplFileInfo; public getFilename(): string; public getGroup(): int|false; public getInode(): int|false; public getLinkTarget(): string|false; public getMTime(): int|false;; public getOwner(): int|false; public getPath(): string;
  33. class ArmorArray extends \ArrayObject { /** * @param ?int $index

    * @param Armor $armor */ public function offsetSet($index = null, $armor = null): void { if ($armor instanceof Armor) { parent::offsetSet($index, $armor); return; } throw new \TypeError('ArmorArray only accepts Armor'); } }