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

今こそ理解するDI(Dependency Injection) / Understand Dependency Injection of PHP

今こそ理解するDI(Dependency Injection) / Understand Dependency Injection of PHP

438799c739f93d00abc30fa2da33c543?s=128

TwitterID:@rukiadia

March 28, 2021
Tweet

Transcript

  1. ࠓͦ͜ཧղ͢Δ %*ʢ%FQFOEFODZ*OKFDUJPOʣ ʮ1)1FS,BJHJʯ IUUQTGPSUFFKQQIQFSLBJHJ

  2. ܰࣗ͘ݾ঺հ

  3. ࣗݾ঺հ • ࢁాɹ঵ʢ΍·ͩɹ͠ΐ͏ʣ • Twitter: @rukiadia • ίωώτגࣜձࣾͰόοΫΤϯυΤϯδχΞΛ͍ͯ͠·͢ • Railsͷੈք͔ΒPHPͷੈքʹ΍͖ͬͯ·ͨ͠ɻ

  4. ຊ೔ͷൃදͰ໨ࢦ͢ΰʔϧ

  5. ຊ೔ͷΰʔϧ %*ʹೖ໳Ͱ͖ͨঢ়ଶ ʢଏʹݴ͏ɺ׬શʹཧղͨ͠ঢ়ଶʣ Λ໨ࢦ͍͖ͯ͠·͢ɻ

  6. ͓࿩͢Δ͜ͱ w %*ͷ֓࿦ w %*ʹର͢Δޡղ

  7. ͦΕͰ͸ɺຊฤελʔτͰ͢ɻ

  8. %*ͷ֓࿦

  9. DIͷ֓࿦ ͦ΋ͦ΋%*ͱ͸Կͳͷ͔ʁ ղઆΛ͍͔ͭ͘ಡΜͰΈͯ΋ ϐϯͱ͜ͳ͍͠ɺ݁ہΑ͘෼͔Βͳ͍ɻ

  10. DIͷ֓࿦ %*ͱ͸ํ๏࿦Ͱ͢

  11. “Dependency Injection Principles, Practices, and Patterns” Part 1 Putting Dependency

    Injection on the map “Dependency Injection is a set of software design principles and patterns that enables you to develop loosely coupled code.” 11 DIͷ֓࿦
  12. %*ͱ͸ʁ w อकੑͷߴ͍ίʔυΛॻ͘͜ͱΛ໨తͱͨ͠ɺιϑτ΢ΣΞઃܭݪଇ ͱύλʔϯͷηοτɻ w ιϑτ΢ΣΞઃܭݪଇͷͭͱͯ͠ɺΏΔ΍͔ͳ݁߹ʢૄ݁߹ʣΛ࡞ ΔΞϓϩʔν͕͋Δɻ w %*͸ʮૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠σβΠϯύλʔϯʯͱ͍͏ͷ͕ద੾ ͳදݱͱݴ͑Δɻ

    w ʮ%*ύλʔϯʯͱݺশ͍ͯ͠Δਓ΋͍Δɻ
  13. %*͕ੜ·Εͨʢఆٛ͞ΕͨʣܦҢʹ ৮Ε͓͖ͯ·͢ɻ DIͷ֓࿦

  14. %*͕ఆٛ͞ΕΔʹࢸͬͨܦҢ

  15. %*ͷىݯΛḷΔ w ىݯ͸+BWBͷίϛϡχςΟ w ೥ʙ೥ࠒʹ.BSUJO'PXMFS͕ఆٛͨ͠ݴ༿ w ˞ࢀߟจݙ͸εϥΠυͷ຤ඌʹࡌͤ·͢ɻ w ʮ*OWFSTJPOPG$POUSPMʢ*P$ʣʯͱ͍͏֓೦͕౰࣌ଘࡏ͓ͯ͠Γɺ ͦͷ֓೦Λ۩ମԽ͢Δݴ༿ͱͯ͠ɺʮ%FQFOEFODZ*OKFDUJPOʯ͕ఆ

    ٛ͞Εͨɻ
  16. *P$ʢ੍ޚͷٯసʣͱ͸Կͳͷ͔ʁ DIͷىݯ

  17. *P$ʢ੍ޚͷٯసʣͦͷ w ϓϩάϥϜʹ͓͍ͯɺʮݺͼग़͢ଆʯͱʮݺͼग़͞ΕΔଆʯ͕ैདྷͷ ϓϩάϥϜͱٯʹͳΔݱ৅Λࢦ͢ɻ w δϣʔΫͱͯ͠ͷҙຯ߹͍ͰʮϋϦ΢ουͷݪཧʯͱݺ͹ΕΔ͜ͱ΋ ͋Δɻ w ʮզʑΛݺͿͳɻզʑ͕ݺͿɻʯ w

    ͚ͩͨ͘ݴ͍ํΛ͢ΔͱɺʮඞཁʹͳͬͨΒɺͪ͜Β͔Βݺͼ· ͢ɻʯͱ͍͏ҙຯ߹͍ʹͳΔɻ
  18. *P$ʢ੍ޚͷٯసʣͦͷ w ϑϨʔϜϫʔΫΛ࢖ͬͨΞϓϦέʔγϣϯ։ൃʹ౰ͯ͸·ΔಛੑͰ΋ ͋Δɻ w ։ൃऀ͕ఆٛͨ͠ϝιου͕ΞϓϦέʔγϣϯίʔυ͔Β௚઀ݺ͹ ΕΔ͜ͱ͸΄΅ແ͍ɻݺ͹ΕΔλΠϛϯά͸ϑϨʔϜϫʔΫଆʹҕ ͶΒΕ͍ͯΔɻ w ʮඞཁʹͳͬͨΒɺͪ͜Β͔Βݺͼ·͢ɻʯͱ͍͏੍ޚ

  19. จষͩͱ఻ΘΓʹ͍͘ͷͰ ίʔυྫΛݟͯΈ·͠ΐ͏ɻ IoCʢ੍ޚͷٯసʣ

  20. ྫɿ$BLF1)1ͷϢχοτςετ class ArticlesTableTest extends TestCas e { public $fixtures =

    ['app.Articles'] ; public function setUp( ) { parent::setUp() ; $this->Articles = TableRegistry::getTableLocator()->get('Articles') ; } public function tearDown( ) { TableRegistry::clear() ; } } هࣄͷݕࡧ΍ফڈΛߦ͏ .PEFMςετͷԼ஍
  21. ྫɿ$BLF1)1ͷϢχοτςετʢ੍ޚͷٯసʣ class ArticlesTableTest extends TestCas e { public $fixtures =

    ['app.Articles'] ; public function setUp() { parent::setUp() ; $this->Articles = TableRegistry::getTableLocator()->get('Articles') ; } public function tearDown( ) { TableRegistry::clear() ; } } ४උɺޙ࢝຤ɺςετ࣮ߦͷλΠϛϯάΛ ϑϨʔϜϫʔΫ͕Ѳ͍ͬͯΔ
  22. Ұ୴ɺ%*ͷ࿩ʹ໭Γ·͢ɻ IoCʢ੍ޚͷٯసʣ

  23. *P$ͱ%*ͷؔ܎ w *P$͸ʮ੍ޚͷٯసʯ w %*͸ݴ͍׵͑Ε͹ʮґଘؔ܎ʹର͢Δ੍ޚͷٯసʯͱදݱͰ͖Δɻ w %*Λʮ੍ޚͷٯసʯͱݴ͍੾ͬͯ΋ӕʹ͸ͳΒͳ͍͕ɺ%**P$ͱ͢ Δʹ͸*P$ͱ͍͏දݱ͕แׅత͗͢Δɻ w ʮґଘؔ܎ʹର͢Δ੍ޚͷٯసʯΛͭͷύλʔϯͱͯ͠ଊ͑Δͨ

    Ίɺ%FQFOEFODZ*OKFDUJPOͱ͍͏໋໊͕ੜ·Εͨɻ
  24. Inversion of Control Dependency Injection DIͷ֓࿦

  25. ϦΞϦςΟͷ͋ΔίʔυΛ·͑ͯ͡ આ໌Λଓ͚·͢ɻ DIͷ֓࿦

  26. ྫɿ&NBJMΛૹ৴͢ΔΫϥε class EmailSender { private $mailClient ; public function init(

    ) { // ʮAmazon Simple Email ServiceʯΛ࢖༻ͯ͠ϝʔϧΛѻ͏Ϋϥε $this->mailClient = new AwsSes( ) } public function sendRegisterMail($address, $text ) { $this->mailClient->send($address, $text) ; } } Ұݟɺ໰୊ͳͦ͞͏ʹݟ͑Δ
  27. ྫɿ&NBJMΛૹ৴͢ΔΫϥε class EmailSender { private $mailClient ; public function init(

    ) { // ʮAmazon Simple Email ServiceʯΛ࢖༻ͯ͠ϝʔϧΛѻ͏Ϋϥε $this->mailClient = new AwsSes() // EmailSenderΫϥε͕AwsSesΫϥεʹґଘ͍ͯ͠Δ } public function sendRegisterMail($address, $text ) { $this->mailClient->send($address, $text) ; } }
  28. &NBJM4FOEFS͕࣋ͭ՝୊ w ಛఆͷαʔϏεʢ͜ͷ৔߹͸"NB[PO4&4ʣʹґଘ͍ͯ͠Δɻ w αʔϏεΛม͑Δඞཁ͕ग़͖ͯͨΒɺ&NBJM4FOEFS಺෦ʹखΛೖΕ ͳͯ͘͸͍͚ͳ͍ɻ w &NBJM4FOEFSΫϥεͷςετʹ"XT4FTΫϥε΋ඞཁʹͳΔɻ w ςετͮ͠Β͍ঢ়ଶʹͳΔɻ

  29. ͯ͞ɺͲ͏͢Δ͔ʁ DIͷ֓࿦

  30. ྫɿ&NBJMΛૹ৴͢ΔΫϥεɾվ class EmailSender { private $mailClient ; public function __construct($client

    ) { $this->mailClient = $client; // αʔϏεΛѻ͏ΫϥεΛ֎෦͔Βࢦఆ͢ΔΑ͏ʹมߋ } public function sendRegisterMail($address, $text ) { $this->mailClient->send($address, $text) ; } } &NBJM4FOEFSͰ࢖༻͢ΔϝιουΛ JOUFSGBDFΛ࢖༻ͯ͠දݱ
  31. &NBJMΛૹ৴͢ΔΠϯλϑΣʔεྫ interface iEmailClient { public function send($address, $text) ; }

    class AwsSes implements iEmailClient { private $mailClient ; public function __construct( ) { $this->mailClient = new AwsSes() ; } public function send($address, $text){ } } &NBJM4FOEFS͕ґଘ͢Δ෦෼Λ JOUFSGBDFͱͯ͠දݱ͠ɺ ґଘ౓ͷ௿ݮΛਤ͍ͬͯΔ
  32. &NBJM4FOEFSΫϥεͷ%* w Ϋϥεࣗ਎͕ґଘઌΛܾΊΔͷͰ͸ͳ͘ɺΫϥεΛѻ͏ଆ͕ґଘઌΛ ܾఆ͍ͯ͠Δɻ w ͜Ε͕ɺґଘؔ܎ʹର͢Δ੍ޚͷٯసɻ w ςετʹඞཁͳϞοΫ͕࡞Γ΍͘͢ͳΓɺςελϏϦςΟ΋޲্ͯ͠ ͍Δɻ

  33. ͏ͬ͢Βͱཧղ͕ਐΜͩͱ͜ΖͰ ࿩୊Λগ͠ม͑·͢ DIͷ֓࿦

  34. %*ʹର͢Δޡղ

  35. %*Λௐ΂Α͏ͱͯ͠ݕࡧ͚ͨ͠Ͳɺ ݁ہΑ͘Θ͔Βͳ͔ͬͨɻ DIʹର͢Δޡղ

  36. ͜Μͳܦݧ͸͋Γ·ͤΜͰ͔ͨ͠ʁ DIʹର͢Δޡղ

  37. %*ʹର͢Δޡղ w %*͸ϢχοτςετΛࢧԉ͢Δͷ͕໨తͰ͋Δɻ w %*ʹ͸%*$POUBJOFSΛ࢖͏ඞཁ͕͋Δɻ w %*͸4FSWJDF-PDBUPSύλʔϯͰ͋Δɻ ޡղ͠΍͍͢ঢ়گ͕ଟ͍

  38. ̍ͭͣͭݟ͍͖ͯ·͠ΐ͏ DIʹର͢Δޡղ

  39. ςετΛࢧԉ͢Δͷ͕໨తͰ͸ͳ͍ w ςελϏϦςΟͷ޲্͸ͭͷଆ໘Ͱ͔͠ͳ͍ɻʮ໨తʯͰ͸ͳ͘ɺ ʮ݁ՌʯͰ͔͠ͳ͍ɻ w %*͸ʮૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠σβΠϯύλʔϯʯͰ͋Δͱɺεϥ Πυͷ๯಄Ͱ΋ड़΂·ͨ͠ɻ w Ϋϥε΍Ϟδϡʔϧؒͷૄ݁߹Λ࣮ݱ͠ɺอकੑͷߴ͍ίʔυΛ࡞Δ ͜ͱ͕ςελϏϦςΟͷ޲্ʹܨ͕Δɻ

  40. %*$POUBJOFS͸ඞਢͰ͸ͳ͍ w ϑϨʔϜϫʔΫଆ͕%*ίϯςφΛ࢖͏લఏͰ࢓૊ΈΛఏڙ͍ͯͯ͠ɺ ։ൃऀଆ͕ޡղͯ͠͠·͏ɻ w ޡͬͨղઆهࣄ΍4UBDL0WFS fl PXΛͨ·ͨ·ݟ͚ͭͯ͠·͏ɻ

  41. %*$POUBJOFSʹ͍ͭͯ গ͚ͩ͠৮Ε͓͖ͯ·͢ DIʹର͢Δޡղ

  42. %*$POUBJOFSͷ؆୯ͳઆ໌ w %*ύλʔϯΛαϙʔτ͢ΔϥΠϒϥϦͰ͢ɻ w ΦϒδΣΫτͷॳظԽ΍߹੒ɺϥΠϑλΠϜͷ؅ཧͱ͍ͬͨ໾ׂΛ ୲͍·͢ɻ w ґଘؔ܎Λఆ͓ٛͯ͘͜͠ͱͰɺΦϒδΣΫτಉ࢜ͷґଘղܾΛ $POUBJOFSଆʹ೚ͤΔ͜ͱ͕Ͱ͖Δɻ w

    ༷ʑͳݴޠʢ$ɺ+BWBɺ1)1ɺ(P౳ʣͰ։ൃ͕͞Ε͍ͯΔɻ
  43. 4FSWJDF-PDBUPSύλʔϯͰ͸ͳ͍ w ͍ͣΕ΋Ϋϥεಉ࢜Λૄ݁߹ʹอͭ͜ͱΛ໨తͱͨ͠σβΠϯύλʔ ϯɻ໨త͸ಉ͕ͩ͡ɺґଘؔ܎ͷղܾख๏͕ҟͳΔɻ w %*͕4FSWJDF-PDBUPSͷҰ෦෼ͱ͍͏࿩Ͱ΋ͳ͍Ͱ͢ɻ w ࠞಉ͠΍͍͢ͷͰɺࠓճͷεϥΠυͷ࢒Γ͸%*ͱ4FSWJDF-PDBUPSύ λʔϯͷൺֱʹׂ͍͍ͯ·͢ɻ

  44. 4FSWJDF-PDBUPSʹ͍ͭͯ

  45. 4FSWJDF-PDBUPSͱ͸ ґଘઌΛղܾ͢Δ໾ׂΛ࣋ͬͨ ΦϒδΣΫτ ServiceLocatorʹ͍ͭͯ

  46. લड़ͨ͠&NBJM4FOEFS Λݩʹͨ͠ਤͰݟͯΈ·͠ΐ͏ ServiceLocatorʹ͍ͭͯ

  47. %*ύλʔϯͰදݱͨ͠Ϋϥεਤ

  48. 4FSWJDF-PDBUPSͰ දݱͨ͠Ϋϥεਤ w 4FSWJDF-PDBUPS͕௥Ճ ͞Ε͓ͯΓɺґଘઌͷ ղܾΛ୲౰͢Δɻ

  49. %*ύλʔϯͱ4FSWJDF-PDBUPSύλʔϯ w %*ύλʔϯ͸ɺ&NBJM4FOEFSΫϥεͷґଘઌΛར༻ݩͰ͋Δ "QQMJDBUJPO͕ܾΊ͍ͯΔɻ w 4FSWJDF-PDBUPS͸ɺ&NBJM4FOEFSΫϥεͷґଘઌΛ4FSWJDF-PDBUPS ͕ܾΊ͍ͯΔɻ w &NBJM4FOEFSͱ"XT$MJFOUΛૄ݁߹ʹ͢Δ໨త͸ͲͪΒͷख๏Ͱ΋࣮ ݱͰ͖Δɻ

  50. ൺֱͨ͠ॴײ w %*ύλʔϯͷํ͕࢖͍΍͍͢ͱࢥ͑·͢ɻ w 4FSWJDF-PDBUPS΁ͷґଘ͕ൃੜ͍ͯ͠ΔͷͰɺςετ͢ΔͨΊʹ 4FSWJDF-PDBUPSͷ࠶ݱʢϞοΫԽʣ͕ඞཁʹͳΔɻ w ґଘؔ܎ͷղܾΛҕͶΔͱ͍͏఺ʹஔ͍ͯ͸ɺ4FSWJDF-PDBUPSͱ%* $POUBJOFS͕ࣅͨ΋ͷʹࢥ͑Δɻ w

    ˞ࣗ෼ͷதͰݴޠԽ͖͠Εͯͳ͍ͷͰɺ͜͜͸ҙݟΛٻΊ͍ͨͱ͜Ζɻ
  51. ຊ೔ͷ·ͱΊ

  52. ຊ೔ͷ·ͱΊ w %*͸ૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠σβΠϯύλʔϯɻ w *OWFSTJPOPG$POUSPMͷͭͷଆ໘ΛσβΠϯύλʔϯͱ໋໊ͯ͠͠ ͨͷ͕ɺʮ%*%FQFOEFODZ*OKFDUJPOʯɻ w %*$POUBJOFS͸%*ύλʔϯΛαϙʔτ͢ΔϥΠϒϥϦɻ w 4FSWJDF-PDBUPS͸%*ͱ໨త͸ಉ͕ͩ͡ɺख๏͕ҟͳΔσβΠϯύ

    λʔϯɻ
  53. ࢀߟจݙ • ʮDependency Injection Principles, Practices, and Patternsʯ • https://www.amazon.com/dp/161729473X

    • ※ O’Reilly online learningͰಡΊ·͢ɻ • https://martinfowler.com/bliki/InversionOfControl.html • https://martinfowler.com/articles/injection.html
  54. %FQFOEFODZ*OKFDUJPO Λཧղ͢Δॿ͚ʹͳΕ͹޾͍Ͱ͢ɻ

  55. ൃදʹର͢ΔϑΟʔυόοΫ͸ େ׻ܴͰ͢ʂ

  56. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ