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

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

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

PHPerKaigi 2022 LT

Kazuhei Arai

April 10, 2022
Tweet

More Decks by Kazuhei Arai

Other Decks in Programming

Transcript

  1. Copyright© M&A
    Predefined Interfaces
    PHPerKaigi
    202 2
    LT

    View full-size slide

  2. Copyright© M&A
    2

    View full-size slide

  3. Copyright© M&A
    3
    M&A CTO


    M&A
    6 🙏

    View full-size slide

  4. Copyright© M&A
    4
    Web Laravel
    3/28 Laravel
    Queue

    View full-size slide

  5. Copyright© M&A
    5

    View full-size slide

  6. Copyright© M&A
    6
    Predefined Interfaces
    Interface
    Interface


    class interface
    interface A {


    public function a();


    }

    View full-size slide

  7. Copyright© M&A
    7
    Predefined Interfaces
    Interface
    Interface
    Iterable
    Cacheable Renderable
    interface Speakable {


    // Ͳ͏஻ΕΔ͔͸෼͔Βͳ͍͕஻ΕΔ


    public function speak();


    }

    View full-size slide

  8. 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

    View full-size slide

  9. Copyright© M&A
    9
    Predefined Interfaces
    Predefined Interfaces
    PHP PHP
    PHP

    View full-size slide

  10. Copyright© M&A
    10
    Predefined Interfaces
    Predefined Interfaces
    Traversable Interface foreach
    Throwable Throwable throw
    Error Exception
    Stringable Interface
    print echo

    View full-size slide

  11. Copyright© M&A
    11
    Predefined Interfaces
    PHP
    Traversable


    Iterator


    IterotorAggregate


    Throwable


    ArrayAccess


    Countable


    Stringable

    View full-size slide

  12. 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ԁ ৽ൃച

    View full-size slide

  13. 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;


    }


    }


    View full-size slide

  14. 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ԁ


    View full-size slide

  15. 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");


    }

    View full-size slide

  16. 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ԁ ৽ൃച


    View full-size slide

  17. Copyright© M&A
    17
    Predefined Interfaces
    Predefined Interface
    echo print throw foreach
    string string string->toJapanese();

    View full-size slide