Slide 1

Slide 1 text

PHPͷ
 Τϥʔͱྫ֎࠶ೖ໳ PHPΧϯϑΝϨϯε෱Ԭ 2017

Slide 2

Slide 2 text

ࣗݾ঺հ • த໺ ୓ (@Hiraku) • https://github.com/hirak • ීஈ͸ϝϧΧϦͰαʔόʔαΠυΤϯδχΞ
 ΍ͬͯ·͢ • Composerؔ࿈ͷൃදΛΑ͍ͯ͘͠·͢

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

PHPͷ
 Τϥʔͱྫ֎࠶ೖ໳ PHPΧϯϑΝϨϯε෱Ԭ 2017

Slide 8

Slide 8 text

ͳΜͰࠓ͞Β
 ʮΤϥʔͱྫ֎ʯʁ

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

ྫ֎͕࿩୊Β͍͠ʂ • 2016೥຤ʙ ҰॠQiita΍ϒϩάͰྫ֎ͷهࣄ ͕૿͑ͨ • http://php-genba.shin1x1.com/1 • ͨͿΜ਒ݯ͸PHPΧϯϑΝϨϯε2016Ͱͷ @t_wada ͞Μͷߨԋ

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

͔Ϳͬͨ… ʘ(^o^)ʗ

Slide 13

Slide 13 text

ྫ֎+PHPͷࣄ৘ ͕ ౰ηογϣϯͰ͢

Slide 14

Slide 14 text

໨࣍ 1. ʮΤϥʔʯͷఆٛ 2. ൃੜͱޙ࢝຤ͷ෼཭ 3. PHPͷΤϥʔͱྫ֎ͷػೳ 4. ྫ֎҆શͱΦϒδΣΫτࢦ޲ 5. ϙέϞϯ໰୊

Slide 15

Slide 15 text

1ষ ʮΤϥʔʯͷఆٛ

Slide 16

Slide 16 text

ྫ:
 HTTPͰϑΝΠϧऔಘ͢Δؔ਺ • $resp = http_get('http://php.net/') • ࣗલ࣮૷͢ΔͳΒɺͲΜͳΤϥʔॲཧΛॻ͖ ·͔͢ʁ

Slide 17

Slide 17 text

http_get(true); // boolean͕౉͞ΕͨΜͰ͚͢Ͳʂ http_get('uso800'); // URL͡Όͳ͍ʂ http_get('http://uso800.example.com/'); // !υϝΠϯݟ͔ͭΒͳ͍Α http_get('http://heavy.example.com/'); // !3࣌ؒͨͬͯ΋ऴΘΒͳ͍ΜͰ͕͢

Slide 18

Slide 18 text

Τϥʔͱ͸ • Կ͔ྑ͘ͳ͍͜ͱ͕ى͖ͨ • ΋͸΍ॲཧΛਐΊΒΕͳ͍ • ʮॲཧΛதࢭ͢Δʯ • ʮԡ͠௨͢ʯ

Slide 19

Slide 19 text

http_get(true); // boolean͕౉͞ΕͨΜͰ͚͢Ͳʂ http_get('uso800'); // URL͡Όͳ͍ʂ http_get('http://uso800.example.com/'); // !υϝΠϯݟ͔ͭΒͳ͍Α http_get('http://heavy.example.com/'); // !3࣌ؒͨͬͯ΋ऴΘΒͳ͍ΜͰ͕͢ ←ॻ͍ͨ΍͕ͭѱ͍ ʢόάʣ ←΋Β͍ࣄނ

Slide 20

Slide 20 text

୭͕ѱ͍͔ • ࢖͍ํ͕ؒҧ͍ͬͯΔύλʔϯ • Ͳ͏ͯ͠΋ൃੜ͠͏Δύλʔϯ
 (࣮ߦ࣌Τϥʔ) • typoͳͷ͔ʁ ࣮ߦ࣌Τϥʔͳͷ͔ʁ • ؔ਺͔Β͸൑ผͰ͖ͳ͍ύλʔϯ

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

PHPͷཱ৔ͰʮΤϥʔʯͱ͸ • PHPࣗମ΋ • ʮιʔείʔυΛड͚औͬͯʯ • ʮ࣮ߦ͢Δʯ • ؔ਺Έ͍ͨͳ΋ͷ • PHPʹͱͬͯԿ͔ྑ͘ͳ͍͜ͱ͕ى͖ͨ
 ʹΤϥʔ

Slide 23

Slide 23 text

PHPʹͱͬͯྑ͘ͳ͍͜ͱ • มͳιʔείʔυ͕౉͞Εͨ • ࣮ߦͯ͠ΈͨΒมͳ͜ͱ͕ى͖ͨ

Slide 24

Slide 24 text

php > "Hello world"Λग़ྗͯ͠Լ͍͞ɻ; PHP Parse error: syntax error, unexpected identifier (T_STRING) in php shell code on line 1 ԿޠͩΑͦΕʂ ཧղෆೳɺॲཧࢭΊΔͶ

Slide 25

Slide 25 text

php > echo Hello; PHP Notice: Use of undefined constant Hello - assumed 'Hello' in php shell code on line 1 Hello ະఆٛఆ਺…
 ͨͿΜ""Λ๨ΕͯΔΜͩͳʂ উखʹจࣈྻѻ͍ʹͯ͠΍Ζ͏
 ͨͩͪ͠ΐͬͱո͍͔͠Β Τϥʔͱͯ͠௨஌

Slide 26

Slide 26 text

php > echo [1,2,3]; PHP Notice: Array to string conversion in php shell code on line 1 Array ഑ྻΛecho…
 ࢓༷ͱͯ͠͸"Array"ͱग़ྗ
 ͢Ε͹͍͍Μ͚ͩͲɺո͍͔͠Β Τϥʔͱͯ͠௨஌

Slide 27

Slide 27 text

PHP͞Μ͸େมͳͷͰ͢ • ίϯύΠϧɺͱ͍͏લॲཧ͕ͳ͍ • ͢΂ͯΛ࣮ߦ࣌ʹॲཧ͠ɺո͚͠Ε͹ͱʹ͔ ͘ΤϥʔΛు͘ • ଞͷݴޠͩͱlint΍ίϯύΠϧ࣌warningʹ૬ ౰͢Δ෦෼΋ʮΤϥʔʯͷൣᙝʹͳΔ

Slide 28

Slide 28 text

PHPͷΤϥʔ৭ʑ • ߏจ͓͔͍͠ (parse error) • ܕએݴҧ൓ • ͪΐͬͱ͋Γ͑ͳ͍҉໧ͷܕม׵ • ഇࢭ༧ఆͷؔ਺Λ࣮ߦͨ͠ (deprecated) • ഇࢭ༧ఆͷߏจΛ࢖͍ͬͯΔ (strict)

Slide 29

Slide 29 text

1ষ ·ͱΊ • PHP͸ιʔείʔυΛͦͷ৔Ͱ࣮ߦͰ͖ͳ͚ Ε͹͍͚ͳ͍ͷͰɺʮΤϥʔʯͷൣғ͕޿͍ • lintʹ͍ۙ಺༰΋ʮΤϥʔʯʹͳΔ

Slide 30

Slide 30 text

2ষ ൃੜͱޙ࢝຤ͷ෼཭

Slide 31

Slide 31 text

࠶ܝ:
 HTTPͰϑΝΠϧऔಘ͢Δؔ਺ • $resp = http_get('http://php.net/') • ࣗલ࣮૷͢ΔͳΒɺͲΜͳΤϥʔॲཧΛॻ͖ ·͔͢ʁ

Slide 32

Slide 32 text

΋͠ɺhttp_getؔ਺ͷ࢓༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒϦτϥΠ͠ଓ͚Δʯ
 ͩͬͨΒʁ • URLΛtypoͨ͠Β

Slide 33

Slide 33 text

΋͠ɺhttp_getؔ਺ͷ࢓༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒۭจࣈΛฦ͢ʯ
 ͩͬͨΒʁ • ຊ౰ʹϨεϙϯε͕ۭจࣈͩͬͨͱ͖ͱݟ෼ ͚͕͔ͭͳ͍

Slide 34

Slide 34 text

΋͠ɺhttp_getؔ਺ͷ࢓༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒϩάʹه࿥ͯ͠falseΛ ฦ͢ʯͩͬͨΒʁ • ϩάͷॻ͖ࠐΈઌͬͯͲ͜Ͱઃఆ͢Δͷʁ

Slide 35

Slide 35 text

ͳʹ͔ؒҧ͍͑ͯΔͷͰ͸…

Slide 36

Slide 36 text

Τϥʔͷޙ࢝຤ͷಛ௃ • ঢ়گʹΑͬͯมΘΓ͕ͪ • ϦτϥΠ • ϩάʹు͍ͯఘΊΔ (ϑΝΠϧ/ඪ४ग़ྗ) • ผͷؔ਺ʹϑΥʔϧόοΫ͢Δ • etc

Slide 37

Slide 37 text

Τϥʔൃੜͷύλʔϯ • ҧ͏ؔ਺Ͱಉ͡Τϥʔ͕ى͖Δ͜ͱ͸සग़ • ྫ) http_get()ͱhttp_post()͸ϗετ໊͕ղܾ Ͱ͖ͳ͚Ε͹ಉ͡Τϥʔ • ͍͍ͪͪॲཧΛॻ͍ͯͨΒίϐϖͩΒ͚ʹͳ Δ

Slide 38

Slide 38 text

ൃੜͱޙ࢝຤Λ ෼཭͠·͠ΐ͏

Slide 39

Slide 39 text

΋͠ɺhttp_getؔ਺ͷ࢓༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒΤϥʔΛ௨஌͢Δʯ(͋ ͑ͯԿ΋ॲཧ͠ͳ͍)ͩͬͨΒʁ • ঢ়گʹԠͯ͡ΤϥʔॲཧΛมߋͰ͖Δ✅ • ΤϥʔॲཧΛڞ௨ԽͰ͖Δ✅

Slide 40

Slide 40 text

ΤϥʔΛ௨஌͢ΔͨΊʹ • શͯͷॲཧ͸தࢭ • ޙ࢝຤Ͱඞཁͳ৘ใͷίϐʔ͕ඞཁ • ʮԿ͕ى͖ͨͷ͔ʯ • ʮͲͷॲཧΛ͍ͯͨ͠࠷தͳͷ͔ʯ

Slide 41

Slide 41 text

Τϥʔͷ௨஌ํ๏͍Ζ͍Ζ • Τϥʔίʔυ(int)Λฦ͢ (Cݴޠͱ͔) • ίʔϧόοΫΛ෼ذ͢Δ(Promise) • ΤϥʔΛλϓϧͰฦ͢ (Goͱ͔) • ྫ֎Λ౤͛Δ

Slide 42

Slide 42 text

ྫ֎Ҏ֎ͷྲྀ೿ͷ
 ࿩͸͠·ͤΜ

Slide 43

Slide 43 text

ྫ֎ΛUISPX͢Δؔ਺ ྫ֎ΛUISPX͢Δؔ਺ ྫ֎ΛUISPX͢Δؔ਺ ຊ൪؀ڥͰ DBUDI͢Δؔ਺ ։ൃ؀ڥͰ DBUDI͢Δؔ਺ Τϥʔ͕ى͖ͨΒ
 ྫ֎Λ౤͛Δ͚ͩ େଟ਺͸ͬͪ͜ ྫ֎Λ෼ੳͯ͠
 ద੾ͳޙ࢝຤Λ͢Δ

Slide 44

Slide 44 text

͋Δؔ਺ͷҰੜ ॲཧ։࢝ ਖ਼ৗऴྃ ؔ਺ݺͼग़͠ ؔ਺ݺͼग़͠

Slide 45

Slide 45 text

ྫ֎͕throw͞ΕͨΒ ॲཧ։࢝ try catch UISPXOFX 999&YDFQUJPO (͜ͷลͷॲཧ͸࣮ߦ͞Εͳ͍ catchʹॻ͍ͯ͋Δॲཧ΁

Slide 46

Slide 46 text

ྫ֎͕΍ͬͯ͘ΕΔ͜ͱ • ✅શͯͷॲཧ͸தࢭ • ✅ޙ࢝຤Ͱඞཁͳ৘ใΛ$eʹؚΊΒΕΔ • ✅͋ͱͰ·ͱΊͯtry ~ catch Ͱ͖Δ

Slide 47

Slide 47 text

2ষͷ·ͱΊ • ΤϥʔॲཧΛ៉ྷʹ͢Δʹ͸ɺൃੜͱޙ࢝຤ Λ෼͚ͯॻ͘ͱΑ͍ • ྫ֎͸͜ͷͨΊͷػೳ

Slide 48

Slide 48 text

3ষ PHPͷΤϥʔͱྫ֎

Slide 49

Slide 49 text


 
 ͷ Τϥʔؔ܎͸ ྺ࢙తܦҢͰෳࡶ

Slide 50

Slide 50 text

ʮΤϥʔʯ
 ͱ ʮྫ֎ʯ

Slide 51

Slide 51 text

PHPͷʮΤϥʔʯ • PHPΤϯδϯࣗମ͕ΤϥʔΛ௨஌͢Δͱ͖ʹ ࢖͏ • ৘ใ͸จࣈྻͷΈ • ໌ࣔతʹൃੜͤ͞Δ͜ͱ΋Ͱ͖Δ • trigger_error("ഇࢭ༧ఆ", E_USER_DEPRECATED)

Slide 52

Slide 52 text

ΤϥʔϨϕϧʹΑͬͯಈ͖͕ҧ ͏ • E_ERROR
 ... ॲཧ͕தஅ͞ΕΔɻ΄΅෮ؼͰ͖ͳ͍ • E_WARNING, E_NOTICE ͦͷଞଟ਺
 ... ॲཧ͸தஅ͞Εͳ͍

Slide 53

Slide 53 text

PHPͷʮྫ֎ʯ • PHP5͔Βಋೖ͞Εͨʮྫ֎ॲཧػߏʯ • ॊೈʹΩϟονͰ͖ͯແࢹͰ͖ͳ͍ • ྫ֎Ϋϥε͸ࣗ༝ʹ֦ுͰ͖Δ • ޷͖ͳ৘ใΛ٧ΊͯOK

Slide 54

Slide 54 text

Τϥʔ ྫ֎ ੲ͔Β͋Δ 1)1Ҏ߱ ΤϥʔϝοηʔδͷΈؚΊΒΕΔ ԿͰ΋ؚΊΒΕΔɺ
 ΦϒδΣΫτ͝ͱ౤͛ΒΕΔ ૊ΈࠐΈͷΤϥʔϨϕϧ͔Β
 બͿ͔͠ͳ͍ ͍͘ΒͰ΋ܕΛ૿΍ͤΔ
 ֦ுͰ͖Δ ઃఆ΍!ԋࢉࢠͰ
 ؆୯ʹແࢹͰ͖Δ ແࢹͰ͖ͳ͍
 ѲΓͭͿ͢ίʔυ͸ॻ͚Δ

Slide 55

Slide 55 text

Ͳ͏ݟͯ΋ ྫ֎ͷํ͕ߴػೳ

Slide 56

Slide 56 text

ΤϥʔΛྫ֎ʹม׵͢Δຐ๏ →΋͸΍Τϥʔ͸ෆཁͳͷʁ

Slide 57

Slide 57 text

ͦΜͳ͜ͱͳ͍Α Τϥʔ͸ศརͳࢠͩΑ

Slide 58

Slide 58 text

Τϥʔͷ௕ॴ=ແࢹͰ͖Δ͜ͱ • ઃఆͰҰׅແࢹ or @ԋࢉࢠͰݸผʹແࢹ • ʮॲཧΛࢭΊΔඞཁ͸ͳ͍͕ɺϓϩάϥϚʹ Ұݴڭ͍͑ͨʯͱ͖͸ͬͪ͜ͷํ͕ศར • E_DEPRECATED͕୅දతͳྫ

Slide 59

Slide 59 text

ϥΠϒϥϦͷAPIΛมߋ͍ͨ͠ ͱ͖ function doSomething() { trigger_error( 'doSomethingͷ୅ΘΓʹdoSomething2Λ࢖ͬͯԼ͍͞', E_USER_DEPRECATED ); return doSomething2(); }

Slide 60

Slide 60 text

deprecatedΛྫ֎ʹ͢Δͱ • ྫ֎͸ແࢹͰ͖ͳ͍ͷͰ… • ݹ͍ؔ਺Λ࢖͍ͬͯΔશ͕ͯյΕΔ • શ෦मਖ਼͠ͳ͍ͱҠߦͰ͖ͳ͍ • मਖ਼ͯ͠Δؒɺ৽ػೳ͕࢖͑ͳ͍ʂ

Slide 61

Slide 61 text

deprecated͕ΤϥʔͳΒ • Ұ୴E_DEPRECATED, E_USER_DEPRECATED Λແࢹ͢Δઃఆʹͯ͠Ξοϓσʔτ • ։ൃ؀ڥͰ༗ޮʹͯ͠ɺൃੜΛ௵͍ͯ͘͠ • ͍ͩͿݮΒͤͨΒɺຊ൪Ͱ΋༗ޮʹͯ͠ɺൃ ੜͷ๾໓Λ֬ೝ͢Δ

Slide 62

Slide 62 text

ଞͷखஈ • /** @deprecated */ • ௨ৗ͸ར༻ऀ͕ಡΜͰ͘Εͳ͍ͱҙຯͳ͍ • error_log • ϩά͸͋͘·ͰจࣈྻͰ͔͠ͳ͍

Slide 63

Slide 63 text

ݸਓతͳ࢖͍෼͚ • ࣗ෼ͷίʔυͰ͸ྫ֎Λ࢖͏ • େ఍ͷPHPΤϥʔ͸ɺErrorExceptionʹม׵͢Δ • ͨͿΜόάͳͷͰಠࣗʹcatch͠ͳ͍ • ͨͩ͠ɺE_DEPRECATEDܥͳͲ͸ྫ֎ʹ͠ͳ͍ ํ͕ศར

Slide 64

Slide 64 text

set_error_handlerୈೋҾ਺ set_error_handler( function(...){ throw new ErrorException(...); }, E_ALL ^ E_DEPRECATED ^ E_USER_DEPRECATED ^ E_USER_NOTICE ); ϋϯυϦϯάͨ͘͠ͳ͍ ΤϥʔϨϕϧΛ আ֎͓ͯ͘͠

Slide 65

Slide 65 text

ΤϥʔϨϕϧ࢖͍෼͚ • E_ERROR ... PHP7Ͱྫ֎ʹͳͬͨ • E_WARNING, E_NOTICE ... େ఍ɺΫιίʔυ͕Ҿ͖ى͜͠ ͍ͯΔͷͰྫ֎ԽɻଈࠁࢭΊͯσόοά • E_STRICT, E_DEPRECATED ... ແࢹͯ͠΋ಈ͘ͷͰྫ֎Խ͠ ͳ͍ɻඞཁʹԠͯ͡؂ࢹɺσόοά • E_USER_XXXX .. ͋͑ͯΤϥʔΛ࢖͍ͬͯΔྫͳͷͰɺྫ֎ ʹ͠ͳ͍͍͔ͯ͘΋ɻ

Slide 66

Slide 66 text

3ষ·ͱΊ • PHPʹ͸ʮΤϥʔʯͱʮྫ֎ʯ͕͋Δ • Τϥʔ͸؆୯ͳ௨஌ͱͯ͠ศར • ྫ֎͸ແࢹͰ͖ͳ͍ • جຊతʹ͸ྫ֎Λ࢖͓͏

Slide 67

Slide 67 text

4ষ ྫ֎҆શͱΦϒδΣΫτࢦ޲

Slide 68

Slide 68 text

ྫ֎ͷϓϥε໘ • ίʔυதʹΤϥʔॲཧ͕ొ৔͠ͳ͍ͷͰɺ
 ៉ྷʹͳΔ • ʮίʔυఀࢭʯΛࣗಈͰ΍ͬͯ͘ΕΔ • ແࢹ͢ΔͱΫϥογϡ͢ΔͷͰແࢹͰ͖ͳ͍ɻ ΤϥʔॲཧΛ͖ͬͪΓߦ͏ίʔυΛཁٻ͢Δɻ

Slide 69

Slide 69 text

ྫ֎ͷͳ͍ੈք $ret = doSomething(); if ($ret === false) { // Τϥʔॲཧ͍ͬͺ͍ return false; } $ret2 = doSomething2(); if ($ret2 === false) { // Τϥʔॲཧ͍ͬͺ͍ return false; }

Slide 70

Slide 70 text

ྫ֎ͷ͋Δੈք $ret = doSomething(); $ret2 = doSomething2(); // ... Ͳ͔͜ݺͼग़͠ͷ
 ্ྲྀͷํͰҰ౓͚ͩ
 try { } catch { } ͯ͋͠Ε͹OK

Slide 71

Slide 71 text

࠶ܝɿ ྫ֎͕throw͞ΕͨΒ ॲཧ։࢝ try catch (͜ͷลͷॲཧ͸࣮ߦ͞Εͳ͍ catchʹॻ͍ͯ͋Δॲཧ΁ UISPXOFX 999&YDFQUJPO

Slide 72

Slide 72 text

͓ؾ͖ͮͩΖ͏͔…

Slide 73

Slide 73 text

ྫ֎͕throw͞ΕͨΒ ॲཧ։࢝ try UISPXOFX 999&YDFQUJPO catch (͜ͷลͷॲཧ͸࣮ߦ͞Εͳ͍ த్൒୺ͳͱ͜ΖͰࢭ·͚ͬͨͲɺ
 ຊ౰ʹେৎ෉ͳͷ͔ʁ catchʹॻ͍ͯ͋Δॲཧ΁

Slide 74

Slide 74 text

ྫ֎ͷϚΠφε໘ • ͍ͭͲ͜Ͱॲཧ͕தஅ͞ΕΔ͔Θ͔Βͳ͍ • Ͳ͜Ͱதஅ͞Εͯ΋ΞϓϦέʔγϣϯ͕յΕͳ ͍Α͏ʹؾΛ͚ͭΔඞཁ͕͋Δ • յΕͳ͍ੑ࣭Λʮྫ֎҆શʯͱݴ͏ • ྫ֎Λѻ͏ਓ͸ɺ
 ৗʹҙࣝ͠ͳ͚Ε͹ͳΒͳ͍

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

ʮྫ֎҆શʯΛάάΔͱ • C++ͷ࿩͕ଟ͍ • ྫ֎҆શҧ൓=ଈࢮͷੈք • ͨͩɺݴޠؔ܎ͳ͘ॏཁͰ͢ʂ • ྫΛڍ͛·͢

Slide 77

Slide 77 text

ྫ1

Slide 78

Slide 78 text

ِͷτϥϯβΫγϣϯ $db->insert($a); doSomething(); $db->insert($b); ... ͜͜Ͱྫ֎͕ى͖ͨΒ
 த్൒୺ͳϨίʔυ͕Ͱ͖ͪΌ͏

Slide 79

Slide 79 text

DBૢ࡞தͷྫ֎ • DBૢ࡞ͷ݁Ռ͕࢒ͬͯ͠·͏ • ෆ۩߹ͷݪҼʹͳΔ • ૢ࡞͕ਖ਼ৗʹऴΘ͔ͬͨɺԿ΋ͳ͔͔ͬͨɺ ͲͪΒ͔ʹ͢Δ = DBͷτϥϯβΫγϣϯػೳ Λ࢖͏

Slide 80

Slide 80 text

[ྫ֎҆શ]τϥϯβΫγϣϯ $db->beginTransaction(); try { $db->insert($a); doSomething(); $db->insert($b); $db->commit(); } catch (\Exception $e) { $db->rollback(); throw $e; }

Slide 81

Slide 81 text

ྫ2

Slide 82

Slide 82 text

κϯϏΩϟογϡ if (self::$cache) return self::$cache; $result = doSomething1(); self::$cache = $result; doSomething2($result); ͜͜Ͱྫ֎͕ى͖ͨΒʁ Ωϟογϡ࢒ͬͯ͠·͏͚Ͳʁ

Slide 83

Slide 83 text

ण໋ͷ௕͍ม਺ͷѻ͍ʹ஫ҙ • Ϋϥεͷstaticม਺ɺάϩʔόϧม਺ • ྫ֎Ͱதஅ͞Εͯ΋ੜ͖࢒ͬͨ·· • ྫ֎ൃੜΛτϥοϓͯ͠࡟আ͢Δඞཁ͕͋Δ ͔΋

Slide 84

Slide 84 text

ྫ3

Slide 85

Slide 85 text

Cݴޠ෩ͷؔ਺ ob_start(); doSomething(); $output = ob_get_clean(); ͜͜Ͱྫ֎͕ى͖ͨΒʁ PC@HFU@DMFBO ݺ͹Εͳ͘Ͷʁ

Slide 86

Slide 86 text

ͦ΋ͦ΋૊ΈࠐΈͷؔ਺͕ ྫ֎҆શ͡Όͳ͍ • ob_start / ob_clean • fopen / fclose • ؔ਺͕ରʹͳ͍ͬͯΔྫ͕݁ߏ͋Δ

Slide 87

Slide 87 text

PHPࣗମ͕कͬͯ͘ΕΔ҆શੑ • ؔ਺ϩʔΧϧͳม਺͸GC͕ճऩͯ͘͠ΕΔ • fopenͳͲͷϦιʔεܕ΋ࣗಈͰด͡ΒΕΔ • ͔͠͠ʮstaticม਺ʯʮ֎෦γεςϜʯʮDBʯ ͳͲ͸͜ͷൣғʹؚ·Εͳ͍ • ѻ͏ਓ͕ྫ֎҆શΛҙࣝ͢Δ͔͠ͳ͍

Slide 88

Slide 88 text

ྫ֎҆શͷ೉͠͞ • ྫ֎҆શ͔Ͳ͏͔͸ػցతʹ൑அͮ͠Β͍ • ίʔυʹؔΘΔਓશһ͕कΔඞཁ͕͋Δ • ͜Ε͕೉͗͢͠Δ

Slide 89

Slide 89 text

ྫ֎҆શΛ࣮ݱ͢Δ ΠσΟΦϜू

Slide 90

Slide 90 text

RAII (scope & destructor) class Curl { function __construct() { $this->ch = curl_init(); } function __destruct() { curl_close($this->ch);
 } }

Slide 91

Slide 91 text

RAII (scope & destructor) function req() { $ch = new Curl(); //... //... } Ҏ߱ɺͲ͜Ͱॲཧ͕
 தஅͯ͠΋σετϥΫλ͕
 ޙ࢝຤ͯ͘͠ΕΔͷͰฏؾ

Slide 92

Slide 92 text

RAIIͷϋϚΓͲ͜Ζ • େ఍͸͏·͍͘͘ͷ͕ͩɺExceptionͷελο ΫτϨʔεͳͲʹั·Δͱɺण໋͕৳ͼΔ • ࢥͬͨλΠϛϯάͰclose͞Εͳ͘ͳΔ͔΋

Slide 93

Slide 93 text

޿ൣғͰcatchͯ͠re-throw try { ... } catch (Exception $e) { ... //ޙ࢝຤Λॻ͘ throw $e; }

Slide 94

Slide 94 text

࠷ॳ͔ΒfinallyͰॻ͘ try { $ch = curl_init(); //... } finally { curl_close($ch); }

Slide 95

Slide 95 text

ྫ֎ͱਖ਼͘͠෇͖߹͏ ͷ͸೉͍͠

Slide 96

Slide 96 text

͔͠͠ ʮྫ֎ࣗମΛ࢖Θͳ͍ʯ ͷ΋೉͍͠

Slide 97

Slide 97 text

4ষ ྫ֎҆શͱΦϒδΣΫτࢦ޲

Slide 98

Slide 98 text

ྫ֎ͱΦϒδΣΫτࢦ޲ • ຊདྷɺྫ֎͸ΦϒδΣΫτࢦ޲ͱશؔ͘܎ͷͳ ͍֓೦ • PHP͸ExceptionΫϥεΛܧঝͨ͠ΦϒδΣΫτ ͔͠throwͰ͖ͳ͍͚Ͳ • 400 ͱ͔ "Τϥʔ͕ى͖·ͨ͠" ͱ͔Λ௚઀throw Ͱ͖Δݴޠ΋͋Δ

Slide 99

Slide 99 text

ΦϒδΣΫτࢦ޲ͯ͠Δͱ • ίϯετϥΫλΛଟ༻ͨ͘͠ͳΔ͸ͣ • த్൒୺ͳঢ়ଶ͕ͳ͍͜ͱ͕อূͰ͖Δ • ίϯετϥΫλ͸ͦͷ࢓্༷ɺ
 Τϥʔ௨஌ʹྫ֎͔͠࢖͑ͳ͍ • return false ͕Ͱ͖ͳ͍͔ΒͶ

Slide 100

Slide 100 text

ྫ) PDO • σϑΥϧτͩͱྫ֎Λు͔ͳ͍ઃఆ • $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); • ͔͠͠ɺnew PDO͢Δͱ͖ͷΤϥʔ͸ɺઃఆ ʹؔ܎ͳ͘ৗʹ1%0&YDFQUJPO

Slide 101

Slide 101 text

࣮ࡍʹ࣮ߦͨ͠ྫ php > $pdo = new PDO('uso800'); PHP Warning: Uncaught PDOException: invalid data source name in php shell code:1 Stack trace: #0 php shell code(1): PDO->__construct('uso800') #1 {main} thrown in php shell code on line 1

Slide 102

Slide 102 text

ͦͷଞͷࣄ৘ • PHP7ͰE_ERROR͕ྫ֎ͷΑ͏ͳ;Δ·͍Λ ͢ΔΑ͏ʹͳͬͨ • ݴޠ૊ΈࠐΈػೳ΋ঃʑʹΤϥʔ͔Βྫ֎ʹ ஔ͖׵͍͑ͯͧ͘એݴ͕ग़͍ͯΔ • ΋͏ɺྫ֎ͷൃੜ͠ͳ͍ੈքͳͲPHPʹ͸ ͳ͍

Slide 103

Slide 103 text

զʑ͸ྫ֎ͱ
 ڞʹੜ͖Δ͔͠ͳ͍ͷͰ͢

Slide 104

Slide 104 text

4ষ·ͱΊ • PHPer΋ྫ֎҆શΛҙࣝ͠·͠ΐ͏ • ΫϥεΛ࢖͑͹ྫ֎Λ࢖͏Ҏ֎બ୒ࢶ͕ͳ͍ ͷͰɺݡ͘෇͖߹͍͖ͬͯ·͠ΐ͏

Slide 105

Slide 105 text

5ষ ϙέϞϯ໰୊

Slide 106

Slide 106 text

try { doSomething(); ... } catch (Exception $e) { // do nothing }

Slide 107

Slide 107 text

Pokémon catching • ΍ͨΒ޿͍ྫ֎ΫϥεͰcatch͍ͯ͠Δ͜ͱͷ ଏশ • RuntimeException/Exception/Throwable • (ϙέϞϯͷΩϟονϑϨʔζʮCatch 'em allʯ͔Β࿈૝ͨ͠ͱࢥΘΕΔʣ

Slide 108

Slide 108 text

Pokémon catchingΛ ΍ͬͯ͸͍͚ͳ͍

Slide 109

Slide 109 text

try { doSomething(); ... } catch (Exception $e) { // do nothing }

Slide 110

Slide 110 text

ԿΛั·͍͑ͨͷ…ʁ • ྫ֎͸͍ΖΜͳؔ਺Ͱൃੜ͢ΔՄೳੑ͕͋Δ • PHPࣗ਎ͷΤϥʔ͔΋͠Εͳ͍ • ͋ͳͨ͸ຊ౰ʹશͯΛద੾ʹޙॲཧͯ͠Δʁ • ͦ͏Ͱͳ͚Ε͹ɺࣗ෼ͷҙਤͨ͠ྫ֎ͷΈʹ ߜ͓ͬͯ͘

Slide 111

Slide 111 text

set_error_handlerͱͷ ૊Έ߹Θͤ • PHPΤϥʔΛErrorExceptionʹม׵͢Δ͜ͱ ͕Ͱ͖Δ • Pokémon catching͕૊Έ߹Θ͞Δͱ஍ࠈ

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

ྫ֎ॲཧʹר͖ࠐ·Εͯtypo ͷҐஔ͕Θ͔Βͳ͍…ʂ

Slide 114

Slide 114 text

୤ɾPokémon catching • ྫ֎͸ࣗ෼ͷͨΊʹ༻ҙ͢Δ & ͦΕͷΈΛ catch͢Δ • class HogeException extends \RuntimeException • setterΛ૿΍͠·͠ΐ͏

Slide 115

Slide 115 text

Guzzleͷྫ

Slide 116

Slide 116 text

ޙ࢝຤͢ΔਓͷͨΊʹ • ඞཁͦ͏ͳ஋ΛExceptionΫϥεʹ٧ΊΒΕΔ Α͏ʹ͢Δ • throw (new HogeException($message, $code))
 ->setFoo($foo)
 ->setMoo($moo);

Slide 117

Slide 117 text

ͱ͸͍͑ɺຊ൪Ͱcatch͞Εͳ ͍ྫ֎͕ى͖ͨΒϠόΠ…ʂ

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

อݥͷset_exception_handler • catch͞Εͳ͔ͬͨྫ֎Λั·͑Δ࠷ޙͷࡆ • େͨ͜͠ͱ͸ग़དྷͳ͍ͷͰɺϩΪϯάͯ͠Ϣʔ βʔʹΤϥʔը໘ΛݟͤΔ͚ͩɺͱ͔ • ຊ൪ͷΈಈ͔͢ɺͳΜͯ͜ͱ΋Մೳ

Slide 120

Slide 120 text

͜Ε͑͋͞Ε͹ྫ֎͸ಥ͖ൈ͚ ͳ͍ set_exception_handler( function (Throwable $e) { echo 'Τϥʔ͕ى͖·ͨ͠'; exit; } );

Slide 121

Slide 121 text

શମͷ·ͱΊ

Slide 122

Slide 122 text

Τϥʔͱྫ֎ͷ࠶ೖ໳ • PHPͷΤϥʔ͸ແࢹͰ͖Δ͜ͱʹՁ஋͕͋Γ· ͢ɻdeprecatedͱͯ͠ͷ࢖͍ํ͸Φεεϝ • ྫ֎͸ศརͰ͕͢ɺྫ֎҆શΛҙࣝ͠ͳ͚Ε͹͍ ͚ͳ͍͜ͱ͸PHPͰ΋มΘΓ·ͤΜ • Exception΍ThrowableΛอݥ໨తҎ֎Ͱcatch͠ ͳ͍Α͏ʹ͠·͠ΐ͏

Slide 123

Slide 123 text

We are hiring!