DI Container Museum@PHP
by
Sota Sugiura
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
%*$POUBJOFS.VTFVN !1)1 !TPUB :"1 BDIJNPO $"TJB)BDIJPKJ !
Slide 2
Slide 2 text
ࣗݾհ w ਿӜᰜଠ 4PUB4VHJVSB w !TPUB w גࣜձࣾΞΠελΠϧ w *1)1+BWB4DSJQU
Slide 3
Slide 3 text
ຊͷ
Slide 4
Slide 4 text
%FQFOEFODZ*OKFDUJPO $POUBJOFS
Slide 5
Slide 5 text
ͦͷલʹʜ ͦͦ%FQFOEFODZ*OKFDUJPOͬͯͳΜͧ
Slide 6
Slide 6 text
%FQFOEFODZ *OKFDUJPO
Slide 7
Slide 7 text
ίϯϙʔωϯτؒͷґଘΛ ؇͢Δ
Slide 8
Slide 8 text
6TFSQIQ db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } }
Slide 9
Slide 9 text
͜͠ΜͳίʔυΛॻ͘ͱ w ͍ΖΜͳ͓͡͞Μʹࡴ͞ΕΔ w ૄ݁߹͓͡͞Μ w ςετ͓͡͞Μ w ΦϒδΣΫτࢦ͓͡͞Μ
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
Կ͕μϝͳͷ͔
Slide 12
Slide 12 text
ϨϏϡʔλΠϜ w %#Ϋϥεͷ"1*͕มΘͬͨΒ6TFSQIQΛಡ· ͳ͖Ό͍͚ͳ͍ w σʔλϕʔε͕มߋ͞ΕͨΒ6TFSQIQΛมߋ͠ ͳ͖Ό͍͚ͳ͍ w ςετ࣌σʔλϕʔεଓΛϞοΫ͍ͨ͠Μͩ ͚ͲͲ͏࣮ݱ͢Δͷʁ
Slide 13
Slide 13 text
db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ %#Ϋϥεͷ"1*มߋͷӨڹΛड͚Δ
Slide 14
Slide 14 text
db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ σʔλϕʔεมߋ࣌ʹ ҾΛॻ͖͑ͳ͚Ε͍͚ͳ͍
Slide 15
Slide 15 text
db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ ͜ͷΛͲ͏ͬͯϞοΫ͢Δͷʁ
Slide 16
Slide 16 text
ґଘؔ w ͭ·Γɺ6TFSΫϥε%#ΫϥεͷมߋͷӨڹ Λड͚͍͢ w 6TFSΫϥε%#Ϋϥεͷґଘ͕ߴ͍
Slide 17
Slide 17 text
꒪⌓꒪
Slide 18
Slide 18 text
ղܾΛࢼΈΔ
Slide 19
Slide 19 text
6TFSQIQ db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } }
Slide 20
Slide 20 text
6TFSQIQ db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } ͜ͷॲཧ͕ݪҼͬΆ͍
Slide 21
Slide 21 text
.BJOQIQ
Slide 22
Slide 22 text
6TFSQIQ db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } }
Slide 23
Slide 23 text
6TFSQIQ db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } } 5ZQF)JOU
Slide 24
Slide 24 text
.BJOQIQ
Slide 25
Slide 25 text
5FTUQIQ
Slide 26
Slide 26 text
ϨϏϡʔλΠϜ w σʔλϕʔε͕มߋ͞Εͯ6TFSΫϥεॻ͖ ͑ͳͯ͘Α͍ w ςετ࣌ʹϞοΫΛίϯετϥΫλʹ͢͜ͱ ͕Ͱ͖ͦ͏ w %#ͷ"1*͕มΘΔͱ6TFSΫϥεΛॻ͖͑ͳ ͖Ό͍͚ͳ͍มΘΒͣ
Slide 27
Slide 27 text
"1*ͷมߋʹڧ͍ΠέͯΔΈ͕ཉ͍͠ P ɻ 㵺㱿㵺
Slide 28
Slide 28 text
*OUFSGBDF
Slide 29
Slide 29 text
8JUIPVU*OUFSGBDF 6TFS %# *OKFDUJPO
Slide 30
Slide 30 text
8JUI*OUFSGBDF 6TFS %#*OUFSGBDF 5ZQF)JOU %# *NQMFNFOU *OKFDUJPO
Slide 31
Slide 31 text
%#*OUFSGBDFQIQ
Slide 32
Slide 32 text
6TFSQIQ db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } } *OUFSGBDF
Slide 33
Slide 33 text
.BJOQIQ
Slide 34
Slide 34 text
ϨϏϡʔλΠϜ w σʔλϕʔε͕มߋ͞Εͯ6TFSΫϥεॻ͖ ͑ͳͯ͘Α͍ w ςετ࣌ʹϞοΫΛίϯετϥΫλʹ͢͜ͱ ͕Ͱ͖ͦ͏ w Πϯελϯεͷ"1*͕มߋ͞Εͯ*OUFSGBDF͞ ͑कΒΕ͍ͯΕॻ͖͑ͳͯ͘Α͍
Slide 35
Slide 35 text
ґଘݮΒͤͨ٩ ˊᗜˋ و
Slide 36
Slide 36 text
*U`T%FQFOEFODZ*OKFDUJPO w ͜Ε͕%*ύλʔϯͷجຊܕ w ΫϥεͷதͰͳ͘ɺ֎Ͱґଘ͢ΔΦϒδΣΫτ Λ࡞Γ֎͔Βೖ͢Δ w ͜ΕҰྫͰɺଞʹ༷ʑͳΞϓϩʔν͕͋Δ
Slide 37
Slide 37 text
,JOEPG%* w ίϯετϥΫλΠϯδΣΫγϣϯ w ηολʔΠϯδΣΫγϣϯ w ϝιουΠϯδΣΫγϣϯ w ϑΟʔϧυΠϯδΣΫγϣϯ
Slide 38
Slide 38 text
ʮͬͦ͘͞ΞϓϦॻ͜͏ʯ
Slide 39
Slide 39 text
$POUSPMMFSQIQ getIndexPage(); } }
Slide 40
Slide 40 text
$POUSPMMFSQIQ getIndexPage(); } } ᶃෳࡶ͗͢Δ
Slide 41
Slide 41 text
$POUSPMMFSQIQ getIndexPage(); } } ᶃෳࡶ͗͢Δ ᶄ݁ہOFXͯ͠Δ
Slide 42
Slide 42 text
δϨϯϚ w %*ύλʔϯʹΑΓۃݶ·ͰґଘΛݮΒ͢͜ͱ Մೳ w ͕ͩ݁ہͲ͔͜ͰΠϯελϯε࡞Βͳ͖Ό͍ ͚ͳ͍ w ΞϓϦέʔγϣϯ͕ڊେʹͳΕͳΔ΄ͲΠϯ ελϯεੜ͕ෳࡶʹͳ͍ͬͯ͘
Slide 43
Slide 43 text
δϨϯϚ w *OUFSGBDFΛ༻ͯ͠ૄ݁߹Խͯ͠Δ߹ந Ϋϥεʹର͢Δ۩ΫϥεͷϚοϐϯά͕ඞཁ w ڊେʹͳΕͳΔ΄Ͳཧ͕ࡶʹ
Slide 44
Slide 44 text
No content
Slide 45
Slide 45 text
ઃఆΛར༻͔Β͠Α͏
Slide 46
Slide 46 text
ઃఆͱΦϒδΣΫτΛ࡞Δํ๏ ར༻ͱΞϓϦέʔγϣϯຊମ
Slide 47
Slide 47 text
ղܾ͍ͨ͜͠ͱ w ΞϓϦͰٻΊΔͷ͕༩͑ΒΕΕಈ͘ίʔ υΛॻ͖͍ͨ w ͲͷΦϒδΣΫτʹԿ͕ೖ͞ΕΔ͔ͷཧ ͳΔͨ͘͘͠ͳ͍
Slide 48
Slide 48 text
͓ɹ·ɹͨɹͤ ͔ͬ͜Βຊฤ
Slide 49
Slide 49 text
%FQFOEFODZ*OKFDUJPO $POUBJOFS
Slide 50
Slide 50 text
%*$POUBJOFS w ΦϒδΣΫτΦϒδΣΫτͷੜํ๏Λอ࣋ ͢Δശ w %*ίϯςφ͕Πϯελϯεͷੜํ๏Λͬͯ ͍Δ w ΞϓϦέʔγϣϯ͕%*ίϯςφ͔Βඞཁͳͷ ΛऔΓग़͚ͩ͢
Slide 51
Slide 51 text
%*$POUBJOFSπʔϧ w %*σβΠϯύλʔϯ͕ͩ%*ίϯςφσβΠ ϯύλʔϯͰͳ͍ w %*ύλʔϯΛ࠾༻࣮ͨ͠Λڧྗʹޙԡ͢͠Δ ͨΊͷπʔϧͱࢥͬͯྑ͍ w ͳͷͰ༷ʑͳΞϓϩʔν͕͋Δ
Slide 52
Slide 52 text
ཁ͢Δʹ %*$POUBJOFS (FU6TFS4FSWJDF "QQMJDBUJPO
Slide 53
Slide 53 text
ཁ͢Δʹ %*$POUBJOFS "QQMJDBUJPO ΰχϣͬͱ ΠϯελϯεΛ࡞Δ
Slide 54
Slide 54 text
ཁ͢Δʹ %*$POUBJOFS "QQMJDBUJPO 6TFS4FSWJDF ͞Εͨ6TFS4FSWJDF Πϯελϯε͕ฦͬͯ͘Δ Πϯελϯεͷੜํ๏Λ ΒͣʹखʹೖΕΔ
Slide 55
Slide 55 text
γϯϓϧʹߟ͑Α͏ w %*ίϯςφͷجຊػೳొͱऔಘ w ͖ͳΦϒδΣΫτΛొ͢Δ w ొͨ͠ΦϒδΣΫτΛऔಘ͢Δ
Slide 56
Slide 56 text
͍͍ͬͦͨ͘͞ʂ ͲΜͳͷ͕͋ΔΜͰ͔͢ʁ Φεεϝʁ
Slide 57
Slide 57 text
1)1%*$POUBJOFST w QJNQMF w BVSBEJ w 1)1%* w %JDF w UIFQIQMFBHVF w TZNGPOZ w "VSZO w [FOETFSWJDFNBOBHFS w BOEFUD
Slide 58
Slide 58 text
1)1%*$POUBJOFST w QJNQMF w BVSBEJ w 1)1%* w %JDF w UIFQIQMFBHVF w TZNGPOZ w "VSZO w [FOETFSWJDFNBOBHFS w BOEFUD ଟ͍
Slide 59
Slide 59 text
ϥΠϒϥϦ࣠Ͱͳ͘ ख๏࣠Ͱ͝Ҋ
Slide 60
Slide 60 text
ϥΠϒϥϦ࣠Ͱͳ͘ ख๏࣠Ͱ͝Ҋ 1JNQMF "VSB 4ZNGPOZ 1)1%* ̃䘞☏匝Օֆָս֡֒Ւ媈䭚ՂթՄ
Slide 61
Slide 61 text
ίϯςφͷొ
Slide 62
Slide 62 text
LFZWBMVFܗࣜͰͷొ w ҙͷͰΦϒδΣΫτΛొ w αʔϏεϩέʔλͱͯ͑͠Δ w ࣗ༝͕ߴ͍ w %*ίϯςφͷجຊͱݴͬͯաݴͰͳ͍ 1JNQMF "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 63
Slide 63 text
LFZWBMVF QJNQMF
Slide 64
Slide 64 text
Ϋϥε໊Ͱͷొ w Ϋϥε໊ͰΦϒδΣΫτΛొ w LFZWBMVFΑΓগ੍͕͠ڧ͍ w %*ύλʔϯΛ࣮ݱ͍͢͠ 1JNQMF "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 65
Slide 65 text
$MBTTOBNF QIQMFBHVF add('SessionInterface', 'Session');
Slide 66
Slide 66 text
-B[ZMPBE Ԇొ w ొ͢ΔࡍʹΦϒδΣΫτΛ࡞Βͳ͍ w औಘ͞ΕΔλΠϛϯάͰΦϒδΣΫτΛੜ w %*ίϯςφͷىಈ࣌ؒͷઅʹͳΔ "VSB 4ZNGPOZ 1)1%* QIQMFBHVF ;FOE
Slide 67
Slide 67 text
γϯάϧτϯ w ΦϒδΣΫτΛγϯάϧτϯͱͯ͠ొ w औಘ͞ΕΔΦϒδΣΫτৗʹಉ͡ w ͍ա͗Δͱςετ͕ࠔʹɻΑ͘ߟ͑Α͏ʂ 1JNQMF "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 68
Slide 68 text
*OKFDU"SHVNFOUT
Slide 69
Slide 69 text
Ҿࢦఆొ w ొ͢ΔࡍʹҾʹԿΛೖΕΔ͔ࢦఆ͢Δ w ίϯετϥΫληολʔɺ5SBJUͳͲʹࢦఆ w நʹରͯ͠ͷ۩Ϛοϐϯά͕Մೳ "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 70
Slide 70 text
$POTUSVDUPS*OKFDUJPO BVSBEJ newInstance(); // Register $di->params['Example']['foo'] = new Foo();
Slide 71
Slide 71 text
4FUUFS*OKFDUJPO BVSBEJ newInstance(); // Register $di->setters['Example']['setFoo'] = new Foo;
Slide 72
Slide 72 text
ϑΟʔϧυΠϯδΣΫγϣϯ w ϓϩύςΟΛηοτ͢Δ w ΞϊςʔγϣϯίϝϯτΛ%*ίϯςφ͕ղऍ ͠ɺඞཁͳΛผ͢Δ w ϞϊʹΑͬͯίϯςφʹґଘ͢ΔͷͰҙ 4ZNGPOZ 1)1%*
Slide 73
Slide 73 text
"OOPUBUJPO 1)1%*
Slide 74
Slide 74 text
1SPQFSUZ 4ZNGPOZ setDefinition('talk', ...); $container->setDefinition('yapc', new Definition( 'Yapc' ))->setProperty('talk', new Reference('talk'));
Slide 75
Slide 75 text
'BDUPSZ w $MPVTFSΛొ͢Δ w ෳࡶͳΠϯελϯεੜΛࢦఆͰ͖Δ w ΠϯελϯεੜΛϑΝΫτϦʔʹ͢Δͷ ͕༰қʹ 1JNQMF "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 76
Slide 76 text
4BNQMF ;FOE [ stdClass::class => InvokableFactory::class, MyObject::class => function (ContainerInterface $c, $name) { return new MyObject($c->get('obj')); }, ] ]);
Slide 77
Slide 77 text
BVUPXJSJOH w %*͕ղऍͰ͖Δ߹ɺશͯࣗಈͰొ͞ΕΔ w ٻΊΔΠϯελϯε͕ඞཁͳґଘΛղܾ͢Δ "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 78
Slide 78 text
4BNQMF BVSBEJ newInstance($builder::AUTO_RESOLVE); // Get instance $userService = $di->newInstance('UserService');
Slide 79
Slide 79 text
ίϯςφ͔Βͷऔಘ
Slide 80
Slide 80 text
LFZWBMVFܗࣜͰͷऔಘ w ҙͷͰΦϒδΣΫτΛऔಘ w ΞϓϦέʔγϣϯͰ͏ͱαʔϏεϩέʔλ ʹͳΔ 1JNQMF "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 81
Slide 81 text
(FUCZLFZOBNF QJNQMF
Slide 82
Slide 82 text
Ϋϥε໊Ͱͷऔಘ w Ϋϥε໊ͰΦϒδΣΫτΛऔಘ w BVUPXJSJOH͕͋Δͷొ͞Εͯͳ͍ͷ औಘͰ͖Δ 1JNQMF "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
Slide 83
Slide 83 text
(FUCZDMBTTOBNF BVSBEJ newInstance(); // Register $di->params['Example']['foo'] = new Foo(); // Get instance $example = $di->newInstance('Example');
Slide 84
Slide 84 text
(FUCZDMBTTOBNF "VSZBO make('UserService');
Slide 85
Slide 85 text
جຊొͱऔಘ w ొͱऔಘͷ֓೦͚ͩΘ͔Ε͍͍ͩͨҰॹ w ΞϓϦέʔγϣϯͷن࣮Ξϓϩʔνɺ ΈͰ%*ίϯςφΛબͿ͠ w ύϑΥʔϚϯε"1*ͷॆ࣮ݟΔͱָ͍͠
Slide 86
Slide 86 text
143
Slide 87
Slide 87 text
143 w 143Ͱ%*$POUBJOFSͷΠϯλʔϑΣʔεΛܾΊ Α͏ͱ͍͏͕͋Δ w ݱࡏͷϑΣʔζ%SBGU w 3FWJFXʹ͕͋Δؾແ͍ؾ͕͢Δɻɻɻ
Slide 88
Slide 88 text
Կ͕ܾΊΒΕ͍ͯΔͷ͔
Slide 89
Slide 89 text
ίϯςφ͔Βͷ औಘΠϯλʔϑΣʔε
Slide 90
Slide 90 text
HFU IBT
Slide 91
Slide 91 text
HFU ΦϒδΣΫτΛऔಘ IBT ΦϒδΣΫτͷ༗ແΛௐΔ
Slide 92
Slide 92 text
DPOUBJOFSJOUFSPQ w ݱ࣌ͷ143Λ࣮ͨ͠ΠϯλʔϑΣʔε w ΦϒδΣΫτͷऔಘํ๏ͷΈఆٛ͞Ε͍ͯΔ w ޙྫ֎͕ͭ w ඇৗʹγϯϓϧ IUUQTHJUIVCDPNDPOUBJOFSJOUFSPQDPOUBJOFSJOUFSPQ
Slide 93
Slide 93 text
BDDMJNBUFDPOUBJOFS w DPOUBJOFSJOUFSPQʹै͍ͬͯΔίϯςφΛ݁ ߹Ͱ͖Δ w ίϯςφͷࠩ͠ସ͑ͷϋʔυϧ͕͘ͳΔ IUUQTHJUIVCDPNKFSFNFBNJBBDDMJNBUFDPOUBJOFS
Slide 94
Slide 94 text
143͍ܾͭ·Δʁ w ਖ਼Θ͔ΒΜ ɾТɾʆ w ͦͦඞཁ͋Δͷ͔ w ࠓDPOUBJOFSJOUFSPQΛಡΊॆ w ͰಡΊ·͢
Slide 95
Slide 95 text
·ͱΊ
Slide 96
Slide 96 text
%*$POUBJOFSͷັྗ
Slide 97
Slide 97 text
ઃఆΛར༻͔Βશʹ Ͱ͖Δ
Slide 98
Slide 98 text
ొͱऔಘͷ֓೦Λ ֮͑ͯ
Slide 99
Slide 99 text
܅͚ͩͷ࠷ڧͷ ΞϓϦέʔγϣϯΛखʹೖΕΖ ٩ ˊᗜˋ و
Slide 100
Slide 100 text
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠