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

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

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

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

Kihara, Takuya

March 30, 2019
Tweet

More Decks by Kihara, Takuya

Other Decks in Technology

Transcript

  1. FizzBuzz໰୊ • 3ͷഒ਺Ͱ “Fizz” ͱग़ྗ • 5ͷഒ਺Ͱ “Buzz” ͱग़ྗ •

    3ͷഒ਺ ͔ͭ 5ͷഒ਺Ͱ “FizzBuzz” ͱग़ྗ
 (ͭ·Γ15ͷഒ਺) • ͦΕҎ֎͸਺஋ࣗମΛจࣈͱͯ͠ग़ྗ ΏΔ8FCࡳຈ
  2. <?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; } }
  3. <?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"; } }
  4. <?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; }
  5. <?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"; } } }
  6. <?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzzPresenterBuilder; for ($num =

    1; $num <= 100; $num++) { echo FizzBuzzPresenterBuilder::build($num) . PHP_EOL; }
  7. <?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); } } }
  8. <?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; } }
  9. ؔ৺ͱ੹೚ • ಛఆͷ੔਺஋Λ༩͑ɺจࣈྻΛग़ྗ͍ͨ͠ɻ
 ˠ ϝιουͷݺͼग़͠ͱग़ྗ • ੔਺஋ͱจࣈྻͷରԠΛอূ͍ͨ͠ɻ
 ˠ ੔਺஋Λड͚औΓɺରԠ͢Δදࣔ಺༰ͷฦ٫ •

    දࣔ಺༰Λ֬ఆ͍ͤͨ͞ɻ
 ˠ ࣗ਎͕ར༻͞ΕΔ৔߹ʹදࣔ಺༰Λฦ٫ ΏΔ8FCࡳຈ ࢖͑Δ͔Ͳ͏͔͸ ʮදࣔ಺༰ʯࣗ਎͕஌Δ΂͖
  10. <?php require_once __DIR__ . '/vendor/autoload.php'; use App\FizzBuzz\FizzBuzzPresenterBuilder; for ($num =

    1; $num <= 100; $num++) { echo FizzBuzzPresenterBuilder::build($num) . PHP_EOL; }
  11. <?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); } }
  12. <?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'); } }