PHPのエラーと例外再入門 / php-error-and-exception
by
Hiraku NAKANO
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
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
ྫ֎ͱΦϒδΣΫτࢦ • ຊདྷɺྫ֎ΦϒδΣΫτࢦͱશؔ͘ͷͳ ͍֓೦ • PHPExceptionΫϥεΛܧঝͨ͠ΦϒδΣΫτ ͔͠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ͰมΘΓ·ͤΜ • ExceptionThrowableΛอݥతҎ֎Ͱcatch͠ ͳ͍Α͏ʹ͠·͠ΐ͏
Slide 123
Slide 123 text
We are hiring!