型を意識したPHPアプリケーション開発

Ca17a082a30f4cbfed1d0a6dacbe3af2?s=47 shin1x1
October 08, 2017

 型を意識したPHPアプリケーション開発

2017/10/08 PHPカンファレンス2017 https://github.com/shin1x1/phpcon2017

Ca17a082a30f4cbfed1d0a6dacbe3af2?s=128

shin1x1

October 08, 2017
Tweet

Transcript

  1. ܕΛҙࣝͨ͠ 1)1ΞϓϦέʔγϣϯ։ൃ ɹ!shin1x1 2017//08 phpcon 2017

  2. https://php-genba.shin1x1.com/

  3. D .BTBTIJ4IJOCBSB!TIJOY ΞδΣϯμ w1)1ͷܕγεςϜ wܕએݴΛར༻ͨ͠ίʔυ࣮૷ wυϝΠϯಛԽܕͷ࣮૷5JQT w͍͞͝ʹ

  4. 1)1ͷܕγεςϜ

  5. &MPRVFOU 3FQPTJUPSZ $foo = 123; echo gettype($foo), PHP_EOL; => integer

    $foo = 'Hello'; echo gettype($foo), PHP_EOL; => string $foo = [1, 2, 3]; echo gettype($foo), PHP_EOL; => array ม਺͸ܕΛ࣋ͨͳ͍ɺ஋͕ܕΛ࣋ͭ
  6. &MPRVFOU 3FQPTJUPSZ echo 10 + 5, PHP_EOL; => 15 ୅਺ԋࢉࢠ͸JOUܕͱqPBUܕ͕ର৅

    ҉໧తܕม׵
  7. &MPRVFOU 3FQPTJUPSZ echo 10 + "5", PHP_EOL; => 15 TUSJOH͕JOUʹม׵͞ΕΔ

    ҉໧తܕม׵
  8. &MPRVFOU 3FQPTJUPSZ echo 10 + "5e", PHP_EOL; => 15 ҉໧తܕม׵

  9. &MPRVFOU 3FQPTJUPSZ echo 10 + "5e2", PHP_EOL; => ??? ҉໧తܕม׵

  10. &MPRVFOU 3FQPTJUPSZ echo 10 + "5e2", PHP_EOL; => 510 ҉໧తܕม׵

  11. &MPRVFOU 3FQPTJUPSZ 5e2 ʢ5=Ծ਺෦, 2=ࢦ਺෦, 10=ج਺ʣ => 5 * (10

    ** 2) => 500 ˞ࢦ਺දه
  12. &MPRVFOU 3FQPTJUPSZ <?php declare(strict_types=1); function double(int $i): int { return

    $i * 2; } double(1); // 2 double(“5e”); // TypeError ܕએݴ
  13. D .BTBTIJ4IJOCBSB!TIJOY 1)1ͷܕγεςϜ w஋͕ܕΛ࣋ͭ w҉໧తܕม׵Ͱॊೈʹ։ൃ wܕએݴͰݫີʹ։ൃ wϢʔβ͕ελΠϧΛબ୒Ͱ͖Δ

  14. ܕΛ׆༻ͨ͠1)1ίʔυͷ࣮૷

  15. D .BTBTIJ4IJOCBSB!TIJOY FY੫ࠐΈՁ֨ͷࢉग़ w঎඼Ձ֨   ফඅ੫཰ 
 খ਺఺ҎԼ͸੾Γࣺͯ w঎඼Ձ֨͸ਖ਼ͷ੔਺

    wফඅ੫཰͸খ਺ͱ͢Δʢʣ
  16. D .BTBTIJ4IJOCBSB!TIJOY %FNP wܕએݴແ͠ wεΧϥʔܕͰܕએݴ wϢʔβఆٛܕͰܕએݴ

  17. D .BTBTIJ4IJOCBSB!TIJOY %FNP https://github.com/shin1x1/phpcon2017

  18. D .BTBTIJ4IJOCBSB!TIJOY υϝΠϯʹಛԽͨ͠ܕΛ࡞Δ wΞϓϦέʔγϣϯυϝΠϯΛදݱ wυϝΠϯϧʔϧΛݕূ wܕએݴʹΑΔදݱͱڧ੍

  19. &MPRVFOU 3FQPTJUPSZ // ܕએݴແ͠ function calculate($price, $taxRate) // υϝΠϯಛԽܕͰܕએݴ function

    calculate( Price $price, TaxRate $taxRate): PriceWithTax
  20. υϝΠϯಛԽܕͷ࣮૷5JQT

  21. D .BTBTIJ4IJOCBSB!TIJOY ҆қʹܧঝΛ࢖Θͳ͍ wܧঝݩΫϥεܕ΁ͷద߹Λ๷͙ wܧঝݩϝιουؚ͕·Εͯ͠·͏ wҕৡ΍τϨΠτͰڞ༗

  22. &MPRVFOU 3FQPTJUPSZ // ܧঝ final class PriceWithTax extends Price {}

    ܧঝ $price = PriceWithTaxCalculator::calculate($price, $taxRate); // PriceWithTax ΋ Price ܕͳͷͰಈ͍ͯ͠·͏ $price = PriceWithTaxCalculator::calculate($price, $taxRate);
  23. D .BTBTIJ4IJOCBSB!TIJOY Πϛϡʔλϒϧʹ͢Δͱ҆શ w׬શίϯετϥΫλ wෆม৚݅ݕূ͕Ұ౓ͰࡁΉ wϛϡʔλϒϧͳΒ౎౓ݕূ

  24. D .BTBTIJ4IJOCBSB!TIJOY ϑΝΫτϦϝιου wίϯετϥΫλΛQSJWBUF wϑΝΫτϦϝιου wҾ਺ʹΑΔόϦΤʔγϣϯ

  25. &MPRVFOU 3FQPTJUPSZ ϑΝΫτϦϝιου public static function of(float $value): self {

    return new self($value); } public static function ofByInt(int $value): self { return new self($value / 100); }
  26. &MPRVFOU 3FQPTJUPSZ ϑΝΫτϦϝιου // float TaxRate::of(0.08); // int TaxRate::ofByInt(8);

  27. D .BTBTIJ4IJOCBSB!TIJOY ։ൃͰͷܕݕࠪ wࣗಈςετ $* w1IQ4UPSN w1IBO

  28. D .BTBTIJ4IJOCBSB!TIJOY 1IQ4UPSN wΤσΟλ
 ϦΞϧλΠϜͳνΣοΫ w*OTQFDU$PEFT
 ϓϩδΣΫτશମΛҰׅνΣοΫ w6TBHFTPG
 ܕͷར༻ҰཡΛྻڍ

  29. D .BTBTIJ4IJOCBSB!TIJOY 1IBO w1)1Ͱ࣮૷͞Εͨ$-*πʔϧ wܕෆҰக΍TUSJDUҧ൓ͳͲΛݕ஌ w$*Ͱܕݕࠪ https://github.com/phan/phan

  30. ͍͞͝ʹ

  31. wܕએݴͰܕΛڧ੍ wυϝΠϯΛܕͰදݱ wܕͰؾ͍͍࣋ͪ։ൃΛ D .BTBTIJ4IJOCBSB!TIJOY ܕΛ׆༻ͨ͠։ൃ

  32. D .BTBTIJ4IJOCBSB!TIJOY !TIJOY !TIJOY D .BTBTIJ4IJOCBSB!TIJOY