Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
極める!メソッドチェーン #phpstudy / fluent-interface
Search
Hiraku NAKANO
July 26, 2017
Technology
8
2.7k
極める!メソッドチェーン #phpstudy / fluent-interface
2017/07/26 第116回 PHP勉強会の発表資料です。
Hiraku NAKANO
July 26, 2017
Tweet
Share
More Decks by Hiraku NAKANO
See All by Hiraku NAKANO
iD決済を支える技術 / #merpay_techtalk
hirak
33
7k
github.com/mercari の今と昔 /github-mercari
hirak
8
3k
PHPStanで始める継続的静的解析 #phperkaigi /php-static-analysis
hirak
14
52k
evalこそパワー #phpkansai / phpkansai2017
hirak
2
1.5k
PHPのエラーと例外再入門 / php-error-and-exception
hirak
19
19k
メルカリ JP Tech Side / mercari-jp-tech-side
hirak
7
1.9k
グローバル展開を支える量子的なサービス設計 #mercariday / mercariday2017-api
hirak
22
13k
Composerプラグインを作ってみよう /phpcon2016
hirak
5
14k
packagist.jpの紹介と新機能 /#phpblt
hirak
4
2.7k
Other Decks in Technology
See All in Technology
駆け出しリーダーとしての第一歩〜開発チームとの新しい関わり方〜 / Beginning Journey as Team Leader
kaonavi
0
120
CDKのコードレビューを楽にするパッケージcdk-mentorを作ってみた/cdk-mentor
tomoki10
0
200
#TRG24 / David Cuartielles / Post Open Source
tarugoconf
0
560
信頼されるためにやったこと、 やらなかったこと。/What we did to be trusted, What we did not do.
bitkey
PRO
0
2.1k
【NGK2025S】動物園(PINTO_model_zoo)に遊びに行こう
kazuhitotakahashi
0
190
Goで実践するBFP
hiroyaterui
1
120
GeometryReaderやスクロールを用いた表現と紐解き方
fumiyasac0921
0
100
2025年のARグラスの潮流
kotauchisunsun
0
790
EMConf JP の楽しみ方 / How to enjoy EMConf JP
pauli
2
140
I could be Wrong!! - Learning from Agile Experts
kawaguti
PRO
8
3.3k
Bring Your Own Container: When Containers Turn the Key to EDR Bypass/byoc-avtokyo2024
tkmru
0
840
ゼロからわかる!!AWSの構成図を書いてみようワークショップ 問題&解答解説 #デッカイギ #羽田デッカイギおつ
_mossann_t
0
1.5k
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
GraphQLとの向き合い方2022年版
quramy
44
13k
The Language of Interfaces
destraynor
155
24k
A designer walks into a library…
pauljervisheath
205
24k
Rails Girls Zürich Keynote
gr2m
94
13k
The Invisible Side of Design
smashingmag
299
50k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
950
YesSQL, Process and Tooling at Scale
rocio
170
14k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Writing Fast Ruby
sferik
628
61k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Transcript
ۃΊΔʂ ϝιουνΣʔϯ @hiraku
ࣗݾհ • த (@Hiraku) • https://github.com/hirak • ϝϧΧϦͷαʔόʔαΠυΤϯδχΞ •
Composerؔ࿈ͷൃදΛΑ͍ͯ͘͠·͢ • PHPΧϯϑΝϨϯεؔ2016 جௐߨԋ
We are hiring!
None
None
None
ۃΊΔʂ ϝιουνΣʔϯ @hiraku
ϝιουνΣʔϯͱ $talk ->setTitle('art of method chain') ->setContent('no plan') ->setLength('20min') ;
ΦϒδΣΫτʹରͯ͠ɺ ϝιουΛଓ͚ͯॻ͚ΔΑ͏ʹ ͨ͠ύλʔϯ/APIσβΠϯͷ͜ͱ
5લʹൃදͨͭ͠ https://www.slideshare.net/hinakano/xml-builder
None
ͪΖΜ߹๏ͳPHPߏจ
Χοίলུ&Մมϝιου໊ ด͡λά͕ͳ͍͜ͱͷ໌ࣔ ด͡λάิ
࠷ۙॻ͍ͨͭ PHPͷߴΦγϟϨةݥྻϥΠϒϥϦspindle/collectionΛ࡞͍ͬͯΔ - Qiita http://qiita.com/Hiraku/items/bb6318d1d2c26ae7f30a
·ΔͰͯ͠ͳ͍…
ϝιουνΣʔϯେ͖ • ੍ͰPHPΒ͘͠ͳ͍ߏจΛ࡞Δઓ • PHPߏจ͕ශऑͩͬͨ • ݺͼग़͠νΣʔϯͰ͖ͳ͍ͱ͔()()()() • །Ұੲ͔Β͑ͨͷ͕ϝιουνΣʔϯ
͓͜ͱΘΓ • ϥΠϒϥϦΛఏڙ͢ΔਓԿΛߟ͑Δ͖͔ʁ Έ͍ͨͳΛ͠·͢ • ҰੲલͷJavaͷম͖͔͠͠Εͳ͍
࣍ 1. σϝςϧͷ๏ଇ 2. ϝιουνΣʔϯͷύλʔϯ৭ʑ 3. APIσβΠϯͷ͓ 4. σϞతͳͭ
1ষ σϝςϧͷ๏ଇ ۃΊΔ!ϝιουνΣʔϯ
σϝςϧͷ๏ଇ (࠷খࣝͷݪଇ) (Law of Demeter, LoD) • ιϑτΣΞͷઃܭɺಛʹΦϒδΣΫτࢦ ϓϩάϥϜͷઃܭʹ͓͚ΔΨΠυϥΠϯ •
ʮ๏ଇʯͱ͍͏༁͕ͩɺʮ͜ΕΛकͬͯΔ ίʔυͷํ͕͍͍Αʂʯͱ͍͏Α͏ͳओࢫ
σϝςϧOK? 0, /( B BCD UIJTB UIJTB C UIJTBC UIJTBC
D
৮ͬͯྑ͍ͷ 0, ϩʔΧϧมɺҾ৮ͬͯ0, B ࣗͷϝιου৮ͬͯ0, UIJTB ࣗͷϓϩύςΟͷϝιου ৮ͬͯ0, UIJTBC
$talk ->setTitle('art of method chain') ->setContent('no plan') ->setLength('20min') ; $talk->setTitle('art
of method chain'); $talk->setContent('no plan'); $talk->setLength('20min'); LoDकͬͨํ͕͍͍ͷʁ ϝιουͷΓ is Կʁ ͜ΕͳΒΓΛؾʹ͢Δ·Ͱͳ͍
ϝιουνΣʔϯͱ $talk ->setTitle('art of method chain') ->setContent('no plan') ->setLength('20min') ;
ΦϒδΣΫτʹରͯ͠ɺ ϝιουΛଓ͚ͯॻ͚ΔΑ͏ʹ ͨ͠ύλʔϯ/APIσβΠϯͷ͜ͱ
͍
ϝιουνΣʔϯLoDҧ • LoDతʹɺ$this->set...; $this->set...; ͱͦΕ ͧΕॻ͖͘ • LoDɺϥΠϒϥϦઃܭऀ͕͓͖ͬͯ͘ ݪଇͷ̍ͭͰ͋Δ
Ұ୴ɺLoDஔ͍ͱ͍ͯɺ ΛਐΊ·͢ɻ
2ষ ϝιουνΣʔϯͷ ύλʔϯ ۃΊΔ!ϝιουνΣʔϯ
LoDΛഁͬͯ·Ͱ ϝιουνΣʔϯͰ ԿΛ͍ͨ͠ͷ͔ʁ
1) return $this class Chainable { function setA($val) { $this->a
= $val; return $this; } function setB($val) { $this->b = $val; return $this; } } (new Chainable) ->setA('a') ->setB('b'); ࿈ଓͰsetͰ͖ΔΑ͏ʹͳͬͨΑʂ
return $this ʹର͢Δݸਓతཧղ • $instance->ͬͯॻ͔ͳͯ͘Α͘ͳΔ • ͪΐͬͱ͚ͩShort hand • ͨͩͦΕ͚ͩͳͷͰɺLoDഁΔ΄ͲͷՁ͕
͋Δ͔…ͱ͍͏ͱඍົͳؾ͢Δ
2) PSR-7ͷAPIΈ͍ͨͳͭ (new GuzzleHttp\Psr7\Request('GET', 'http://example.com')) ->withHeader('foo', 'baa') ->withBody($body); return $thisͰͳ͘ɺ
newͯ͠ࠓ·ͰͷใΛίϐʔ (ผΠϯελϯεΛฦ͢)
immutableͱ • setterͷͳ͍ΦϒδΣΫτ • ෦ΛมߋͰ͖ͳ͍ͷͰɺ clone͠ͳͯ҆͘શʹฦͤΔ • setterͰͳ͘ɺʮίϐʔͯ͠Ұ෦Λ্ॻ͖ʯ ͢ΔσβΠϯ
immutable ʹର͢Δݸਓతཧղ • ΓΛड͚औΒͳ͍ͱ݁Ռ͕ফ͑ͯ͠·͏ • $obj->setFoo($foo); $obj->setBaa($baa); ͯ͠ҙຯ͕ͳ͍ • $obj
= $obj->setFoo($foo); $obj = $obj->setBaa($baa); • ه๏͕໘ͳͷͰɺϝιουνΣʔϯʹͨ͘͠ͳΔ(Θ͔Δ)
ͪΐͬͱ͕ҳΕ·͕ͨ͠ • return $this͢Δ͚͕ͩϝιουνΣʔϯͰ ͳ͍ • return new ͢Δύλʔϯ͋Δ
return newͰ ༡ΜͰΈΔྫ
͓ • ϝιουνΣʔϯͰ ೖΕࢠͰ͖ΔifจΛ ࡞Ε
ϒϩοΫʹΦϒδΣΫτΛͯ ΊΔ • ifnewΛฦ͢ • endifϒϩοΫΛ ऴΘΒͤͯͱʹ Δ
<?php abstract class Context { protected $parent = null; function
__construct(Context $parent=null) { $this->parent = $parent; } abstract function if(bool $bool): Context; abstract function say(string $str): Context; function endif(): Context { return $this->parent; } } class TrueContext extends Context { function if(bool $bool): Context { if ($bool) { return new self($this); } else { return new FalseContext($this); } } function say(string $str): Context { echo $str,PHP_EOL; return $this; } } class FalseContext extends Context { function if(bool $bool): Context { return new self($this); } function say(string $str): Context { return $this; } } function dsl(): TrueContext { return new TrueContext; } ͍͍ͩͨ ͜Ε͙Β͍ Ͱ࡞ΕΔ ϝιουνΣʔϯDSLͰϒϩοΫΛ࡞ΔςΫχοΫ: Architect Note http://blog.tojiru.net/article/272060766.html
3) +ϚδοΫϝιουͷཚ༻ return $thisͰͳ͘ɺ newͯ͠ࠓ·ͰͷใΛίϐʔ (ผΠϯελϯεΛฦ͢)
ϚδοΫϝιου • __get(){}ɺ__call(){}ΛΫϥεʹੜ͓ͯ͘͠ • ϓϩύςΟ/ϝιουʹΞΫηεग़དྷͳ͍ͱ͖ ʹࣗಈతʹݺΕΔ • Rubyͷmethod_missingΈ͍ͨͳͭ
IUNM NFUB@ UJUMF@ @ ͳΜͯϝιου QVCMJDϓϩύςΟੜ͍͑ͯͳ͍ɻ ࣮ࡍʹॲཧ͍ͯ͠Δͷ @@DBMM ͱ@@HFU
ϚδοΫϝιου • ✅ίʔυྔ(ܶతʹݮΔ) • ❌ಡΈ͢͞ • ❌֦ுੑ • ❌PhpStormʹౖΒΕΔ •
ׂͱݏ͍ͬͯΔPHPer͕ଟ͍ҹ(ओ؍)
4) fluent interface • ʮྲྀΕΔΑ͏ͳΠϯλʔϑΣʔεʯ • ϝιουνΣʔϯΛ͏গ͠ൃలͤͨ͞ͷ • આ໌͕͍͠ͷͰ࣍ͷষ
3ষ APIσβΠϯͷ ۃΊΔ!ϝιουνΣʔϯ
ঢ়گ • ͋ͳͨɺͱ͋ΔWebαʔϏεͷPHP൛SDK Λ࡞Δ͜ͱʹͳΓ·ͨ͠ • ͦͷWebαʔϏεେྔͷػೳ͕͋Γ·͢ɻ • SDKͳͷͰɺੜcurlͰୟ͘ΑΓศརʹ͍ͨ͠
None
None
SDKͷAPIσβΠϯ • SDKϢʔβʔ෦࣮ΛΑ͘Βͳ͍ • Ͱ͖ΕυΩϡϝϯτ͚ͩͰ͍͜ͳ͍ͨ͠ • ͲΜͳΫϥε͕͋ͬͯɺ ͲΜͳϝιουΛஔ͢Δʁ
…Ԟͷਂ͍Ͱ͋Δ
ςϨϏͷϦϞίϯ
None
Α͋͘ΔςϨϏͷϦϞίϯ • Ϙλϯ͕͍ͬͺ͍͋Δ • ͲΕΛԡ͍͍ͤͷʁ໎͏ • (ͨͿΜ)ςετେมͦ͏… • ͍ͭͰ͋ΒΏΔϘλϯ͕ԡͤΔ
ղܾҊ • ͍͞͠ΐϘλϯ1ݸ(ిݯ)ͷΈ • ిݯ͕ೖΔͱɺ͏ʹΐʔΜͱϘλϯ͕ੜ͑ͯ͘Δ • ૢ࡞͢ΔͨͼʹɺϘλϯ͕͏ʹΐʔΜͱੜ͑ͯ͘ Δ or ফ͑Δ
• ϘλϯݮΒͤͦ͏Ͱ͢Ͷʂ
API = ϦϞίϯ
APIσβΠϯʹͯΊͯΈΔ • ϩάΠϯ͕ඞཁͳΒɺϩάΠϯͷAPI͚͕ͩ ݟ͑ͯΔ͖ • ͦͷঢ়ଶͰՄೳͳૢ࡞͚͕ͩ͑Δ • linkͰػೳΛͭͳ͙RESTͷΑ͏ͳߟ͑ํ
fluent interfaceͷߟ͑ํ • จ຺(ίϯςΩετ)ʹԠͯ͡ɺreturn͢Δ ΫϥεΛมߋ͢Δ • ඞཁेͳϝιου͚ͩ৮ΕΔঢ়ଶΛอͭ
4ষ SELECTจϏϧμʔ ۃΊΔ!ϝιουνΣʔϯ
ΫΤϦϏϧμͬͯ͋Γ·͢ΑͶ • PHP্ͰSQLΛΈཱͯΔͭ • ΞϨΛfluent interfaceͰઃܭ(design)ͯ͠ Έ·͠ΐ͏ • ࣮·Ͱؒʹ߹Θͳ͔ͬͨ
SELECTจͷ෮श ͍͍ͩͨҎԼͷॱͰॲཧ͞ΕΔ FROM → JOIN → WHERE → GROUP BY
→ HAVING → PROJECT → ORDER BY → LIMIT → UNION ࢦఆॱΛ͜ΕͰݻఆͰ͖ΔΑ͏ʹAPIઃܭ
ࡉ͔͘ΫϥεΛ༻ҙ
ΓΛ࣍ͷίϯςΩετʹ
ͱ͍͏Θ͚Ͱదʹ࡞ͬͨͭ https://github.com/hirak/php-query-builder (APIσβΠϯͷͳͷͰதϞοΫঢ়ଶ)
Demo
fluent interfaceͷ͍͍ͱ͜Ζ • IDEͷิͱΈ߹ΘͤΔͱޮՌ͙ͭΜͩ
APIઃܭऀͷ಄ͷத • LoDʹै۪ͬͨͳઃܭʹ͢Δ͖͔ʁ • ίϯςΩετΛಋೖͯ͠fluent͗Έʹ͢Δ͔ʁ • ΞϓϦͷUIΛߟ͑Δ͜ͱͱࣅ͍ͯΔ • ਖ਼ղͳ͍͚Ͳɺ͍Ζ͍ΖͳྲྀΛ͓ͬͯ ͘ͱɺཱ͍͔͔ͭͭʁ
·ͱΊ
Ԟਂ͖ϝιουνΣʔϯͷੈք • σϝςϧͷ๏ଇ͓͖֮͑ͯ·͠ΐ͏ • ϝιουνΣʔϯͰDSLͬΆ͍ͷ࡞Δͱ ָ͍͠Α • return $thisҎ֎ʹ৭ʑ͋Δͷ͓֮͑ͯ͘ ͱ͍͍Α
ͦͷଞͷ
Unified Variable Syntax (PHP7) • PHP7͔ΒɺνΣʔϯͰ͖ΔߏจͷόϦΤʔ γϣϯ͕૿͑ͯΔ • abc()()ɺBaa::a()::foo() •
Կ͔͍ํͳ͍͔ͳʔɻɻ