Kihara, Takuya
March 30, 2019
1.2k

# PHPで FizzBuzz問題をやってみよう #ゆるWeb札幌

PHPでFizzBuzz問題を解いていく。
シンプルな解き方から、段階的にオブジェクト指向を取り入れてみる。
ゆるWeb勉強会@札幌 #4
#ゆるWeb札幌

March 30, 2019

## Transcript

6. ### FizzBuzz໰୊ • 3ͷഒ਺Ͱ “Fizz” ͱग़ྗ • 5ͷഒ਺Ͱ “Buzz” ͱग़ྗ •

3ͷഒ਺ ͔ͭ 5ͷഒ਺Ͱ “FizzBuzz” ͱग़ྗ  (ͭ·Γ15ͷഒ਺) • ͦΕҎ֎͸਺஋ࣗମΛจࣈͱͯ͠ग़ྗ ΏΔ8FCࡳຈ

11. ### <?php for (\$num = 1; \$num <= 100; \$num++) {

if (\$num % 3 === 0 && \$num % 5 === 0) { echo 'FizzBuzz' . PHP_EOL; } elseif (\$num % 3 === 0 && \$num % 5 !== 0) { echo 'Fizz' . PHP_EOL; } elseif (\$num % 3 !== 0 && \$num % 5 === 0) { echo 'Buzz' . PHP_EOL; } else { echo "\$num" . PHP_EOL; } }

14. ### <?php for (\$num = 1; \$num <= 100; \$num++) {

echo getFizzBuzz(\$num) . PHP_EOL; } function getFizzBuzz(int \$num): string { if (\$num % 3 === 0 && \$num % 5 === 0) { return 'FizzBuzz'; } elseif (\$num % 3 === 0 && \$num % 5 !== 0) { return 'Fizz'; } elseif (\$num % 3 !== 0 && \$num % 5 === 0) { return 'Buzz'; } else { return "\$num"; } }

17. ### <?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzz; \$fizzBuzz = new

FizzBuzz(); for (\$num = 1; \$num <= 100; \$num++) { echo \$fizzBuzz->getFizzBuzz(\$num) . PHP_EOL; }
18. ### <?php namespace App\FizzBuzz; class FizzBuzz { public function getFizzBuzz(int \$num):

string { if (\$num % 3 === 0 && \$num % 5 === 0) { return 'FizzBuzz'; } elseif (\$num % 3 === 0 && \$num % 5 !== 0) { return 'Fizz'; } elseif (\$num % 3 !== 0 && \$num % 5 === 0) { return 'Buzz'; } else { return "\$num"; } } }

20. ### Ϋϥεͷ੹೚? • ͦͷΫϥε͕ʮҰͭͷ੹೚ʯʹด͍ͯ͡Δ͔ • ؔ৺ͷ෼཭ɺૄ݁߹ɺͱ͍ͬͨΩʔϫʔυɻ • 1ʙ100ͷ਺஋ʹରԠ͢ΔจࣈྻΛදࣔ • ਺஋ͱจࣈͷରԠؔ܎ͷ൑ఆ •

ରԠ͢Δจࣈྻͷฦ٫ https://github.com/tacck/PHPFizzBuzzTraining/tree/Step4 ΏΔ8FCࡳຈ

22. ### ؔ৺ͱ੹೚ • ಛఆͷ੔਺஋Λ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ  ˠ ϝιουͷݺͼग़͠ͱ݁Ռͷग़ྗ • ੔਺஋ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ  ˠ ੔਺஋Λड͚औΓɺରԠ͢Δදࣔ಺༰ͷฦ٫ •

දࣔ಺༰Λ֬ఆ͍ͤͨ͞ɻ  ˠ ࣗ਎͕ར༻͞ΕΔ৔߹ʹදࣔ಺༰Λฦ٫ ΏΔ8FCࡳຈ
23. ### ؔ৺ͱ੹೚ • ಛఆͷ੔਺஋Λ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ  ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ੔਺஋ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ  ˠ ੔਺஋Λड͚औΓɺରԠ͢Δදࣔ಺༰ͷฦ٫ •

දࣔ಺༰Λ֬ఆ͍ͤͨ͞ɻ  ˠ ࣗ਎͕ར༻͞ΕΔ৔߹ʹදࣔ಺༰Λฦ٫ ݺͼग़ͯ͠࢖͏ ݺͼग़ͯ͠࢖͏ ΏΔ8FCࡳຈ

Ұํ޲
25. ### <?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzzPresenterBuilder; for (\$num =

1; \$num <= 100; \$num++) { echo FizzBuzzPresenterBuilder::build(\$num) . PHP_EOL; }
26. ### <?php namespace App\FizzBuzz; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzBuzzPresenterBuilder { public static

function build(int \$num): FizzBuzzPresenterInterface { if (\$num % 3 === 0 && \$num % 5 === 0) { return new \App\FizzBuzz\Presenter\FizzBuzzPresenter(\$num); } elseif (\$num % 3 === 0 && \$num % 5 !== 0) { return new \App\FizzBuzz\Presenter\FizzPresenter(\$num); } elseif (\$num % 3 !== 0 && \$num % 5 === 0) { return new \App\FizzBuzz\Presenter\BuzzPresenter(\$num); } else { return new \App\FizzBuzz\Presenter\NumberPresenter(\$num); } } }
27. ### <?php namespace App\FizzBuzz\Presenter; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzPresenter implements FizzBuzzPresenterInterface {

private \$num; public function __construct(int \$num) { \$this->num = \$num; } public function __toString(): string { return 'Fizz'; } public function isAvailable(): bool { return true; } }

31. ### ؔ৺ͱ੹೚ • ಛఆͷ੔਺஋Λ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ  ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ੔਺஋ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ  ˠ ੔਺஋Λड͚औΓɺରԠ͢Δදࣔ಺༰ͷฦ٫ •

දࣔ಺༰Λ֬ఆ͍ͤͨ͞ɻ  ˠ ࣗ਎͕ར༻͞ΕΔ৔߹ʹදࣔ಺༰Λฦ٫ ΏΔ8FCࡳຈ ͜͜ʹ৚݅෼ذ
32. ### ؔ৺ͱ੹೚ • ಛఆͷ੔਺஋Λ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ  ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ੔਺஋ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ  ˠ ੔਺஋Λड͚औΓɺରԠ͢Δදࣔ಺༰ͷฦ٫ •

දࣔ಺༰Λ֬ఆ͍ͤͨ͞ɻ  ˠ ࣗ਎͕ར༻͞ΕΔ৔߹ʹදࣔ಺༰Λฦ٫ ΏΔ8FCࡳຈ ͜͜ʹ஫໨
33. ### ؔ৺ͱ੹೚ • ಛఆͷ੔਺஋Λ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ  ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ੔਺஋ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ  ˠ ੔਺஋Λड͚औΓɺରԠ͢Δදࣔ಺༰ͷฦ٫ •

දࣔ಺༰Λ֬ఆ͍ͤͨ͞ɻ  ˠ ࣗ਎͕ར༻͞ΕΔ৔߹ʹදࣔ಺༰Λฦ٫ ΏΔ8FCࡳຈ ࢖͑Δ͔Ͳ͏͔͸ ʮදࣔ಺༰ʯࣗ਎͕஌Δ΂͖

35. ### <?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzzPresenterBuilder; for (\$num =

1; \$num <= 100; \$num++) { echo FizzBuzzPresenterBuilder::build(\$num) . PHP_EOL; }
36. ### <?php namespace App\FizzBuzz; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzBuzzPresenterBuilder { public static

function build(int \$num): FizzBuzzPresenterInterface { foreach (glob(__DIR__ . '/Presenter/Dynamic/*.php') as \$file) { require_once \$file; \$class = '\App\FizzBuzz\Presenter\Dynamic\\' . basename(\$file, '.php'); \$presenter = new \$class(\$num); try { // throw ParseError if \$num is not available \$presenter->isAvailable(); return \$presenter; } catch (\ParseError \$e) { // none } } return new \App\FizzBuzz\Presenter\NumberPresenter(\$num); } }
37. ### <?php namespace App\FizzBuzz\Presenter\Dynamic; use App\FizzBuzz\FizzBuzzPresenterInterface; class FizzPresenter implements FizzBuzzPresenterInterface {

private \$num; public function __construct(int \$num) { \$this->num = \$num; } public function __toString(): string { return 'Fizz'; } public function isAvailable(): bool { return (\$this->num % 3 === 0 && \$this->num % 5 !== 0) ? true : eval('invalid'); } }

40. ### ·ͱΊ • FizzBuzz໰୊Λɺ  ͍͔ͭ͘ͷܗͰղ͍ͯΈͨɻ • ΦϒδΣΫτࢦ޲ͷߟ͑ํΛ͏·͘࢖͏ͱɺ  ҆શ͔֦ͭுੑΛߴ͘Ͱ͖Δɻ  ˠ ࠓճ࢖ͬͨͷ͸  ɹ

ʮΦʔϓϯɾΫϩʔζυͷݪଇʯ • ಄ͷମૢΛ݉ͶͯɺറΓϓϨΠΛͯ͠ΈΑ͏! ΏΔ8FCࡳຈ
41. ### ·ͱΊ • ੹೚ͷ෼͚ํɺؔ৺ͷ෼཭ɺ  ࠷ॳ͸೉͍͠ɻ • ʮ͜ͷਓ(Ϋϥε)͸ԿΛ஌͍ͬͯΔ΂͖͔ʯ  ʮ͜ͷਓ(Ϋϥε)ʹ͸ԿΛ஻ͬͯཉ͍͔͠ʯ  ਓ(Ϋϥε)ͷؾ࣋ͪʹͳͬͯߟ͑Δɻ • TDD

(ςετۦಈ։ൃ) Λ΍Δͱɺ  ʮΫϥεʹ໰͍͔͚ͳ͕Βʯ։ൃͰ͖Δɻ ΏΔ8FCࡳຈ