Share
Embed
Transcript
17 slides
-
Copyright© M&A 6 Predefined Interfaces Interface Interface class interface interface
A { public function a(); } -
Copyright© M&A 7 Predefined Interfaces Interface Interface Iterable Cacheable Renderable
interface Speakable { // Ͳ͏ΕΔ͔͔Βͳ͍͕ΕΔ public function speak(); } -
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 -
Copyright© M&A 10 Predefined Interfaces Predefined Interfaces Traversable Interface foreach
Throwable Throwable throw Error Exception Stringable Interface print echo -
Copyright© M&A 11 Predefined Interfaces PHP Traversable Iterator IterotorAggregate Throwable
ArrayAccess Countable Stringable -
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ԁ ৽ൃച -
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; } } -
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ԁ -
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"); } -
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ԁ ৽ൃച -
Copyright© M&A 17 Predefined Interfaces Predefined Interface echo print throw
foreach string string string->toJapanese();