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

Predefined Interfacesを使って便利な独自クラスを作りましょう

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Predefined Interfacesを使って便利な独自クラスを作りましょう

PHPerKaigi 2022 LT

Avatar for Kazuhei Arai

Kazuhei Arai

April 10, 2022
Tweet

More Decks by Kazuhei Arai

Other Decks in Programming

Transcript

  1. Copyright© M&A 7 Predefined Interfaces Interface Interface Iterable Cacheable Renderable

    interface Speakable { // Ͳ͏஻ΕΔ͔͸෼͔Βͳ͍͕஻ΕΔ public function speak(); }
  2. Copyright© M&A 8 Predefined Interfaces Interface interface Speakable { //

    Ͳ͏஻ΕΔ͔͸෼͔Βͳ͍͕஻ΕΔ public function speak(); } class Japanese implements Speakable { public function speak() { echo "͜Μʹͪ͸"; } } class American implements Speakable { public function speak() { echo "Hello"; } } Interface
  3. Copyright© M&A 10 Predefined Interfaces Predefined Interfaces Traversable Interface foreach

    Throwable Throwable throw Error Exception Stringable Interface print echo
  4. Copyright© M&A 12 Predefined Interfaces Predefined Interface Stringable(PHP 8 )

    (__toString() ) class Book implements Stringable { public function __construct(private string $name, private int $price, private bool $isNew = false) {} public function __toString() { return $this->name . " : " . $this->price . "ԁ" . ($this->isNew ? " ৽ൃച" : ""); } } echo new Book(name: 'ϓϩϑΣογϣφϧWebϓϩάϥϛϯά Laravelʪ࠷৽Laravel 9ରԠʫ', price: 3520, isNew: true); ~/Project/predefined-interface ▶ php iterator.php ϓϩϑΣογϣφϧWebϓϩάϥϛϯά Laravelʪ࠷৽Laravel 9ରԠʫ : 3520ԁ ৽ൃച
  5. Copyright© M&A 13 Predefined Interfaces Predefined Interface Iterator Traversal foreach

    Iterator Traversal class BookShelf implements Iterator { private int $position = 0; public function __construct(private array $books) {} public function current() { return $this->books[$this->position]; } public function next() { ++$this->position; } public function key() { return $this->position; } public function valid() { return isset($this->books[$this->position]); } public function rewind() { $this->position = 0; } }
  6. Copyright© M&A 14 Predefined Interfaces Predefined Interface Iterator Traversal foreach

    $books = [ new Book(name: 'PHPϑϨʔϜϫʔΫ Laravel WebΞϓϦέʔγϣϯ։ൃ όʔδϣϯ5.5 LTSରԠ', price: 4180), new Book(name: 'ΈΜͳͷPHPɹݱ৔Ͱ໾ཱͭ࠷৽ϊ΢ϋ΢ʂ', price: 2398) ]; $boolShelf = new BookShelf($books); foreach ($boolShelf as $book) { print($book); print("\n"); } ~/Project/predefined-interface ▶ php iterator.php PHPϑϨʔϜϫʔΫ Laravel WebΞϓϦέʔγϣϯ։ൃ όʔδϣϯ5.5 LTSରԠ : 4180ԁ ΈΜͳͷPHPɹݱ৔Ͱ໾ཱͭ࠷৽ϊ΢ϋ΢ʂ : 2398ԁ
  7. Copyright© M&A 15 Predefined Interfaces Predefined Interface class BookShelf implements

    Iterator { … ུ … public function addNewArrival(string $name, int $price) { $this->books[] = new Book($name, $price, isNew: true); } } $books = [ new Book(name: 'PHPϑϨʔϜϫʔΫ Laravel WebΞϓϦέʔγϣϯ։ൃ όʔδϣϯ5.5 LTSରԠ', price: 4180), new Book(name: 'ΈΜͳͷPHPɹݱ৔Ͱ໾ཱͭ࠷৽ϊ΢ϋ΢ʂ', price: 2398) ]; $boolShelf = new BookShelf($books); $boolShelf->addNewArrival(name: 'ϓϩϑΣογϣφϧWebϓϩάϥϛϯά Laravelʪ࠷৽Laravel 9ରԠʫ', price: 3520); foreach ($boolShelf as $book) { print($book); print("\n"); }
  8. Copyright© M&A 16 Predefined Interfaces Predefined Interface ~/Project/predefined-interface ▶ php

    iterator.php PHPϑϨʔϜϫʔΫ Laravel WebΞϓϦέʔγϣϯ։ൃ όʔδϣϯ5.5 LTSରԠ : 4180ԁ ΈΜͳͷPHPɹݱ৔Ͱ໾ཱͭ࠷৽ϊ΢ϋ΢ʂ : 2398ԁ ϓϩϑΣογϣφϧWebϓϩάϥϛϯά Laravelʪ࠷৽Laravel 9ରԠʫ : 3520ԁ ৽ൃച