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
PHPのエラーと例外再入門 / php-error-and-exception
Search
Hiraku NAKANO
June 10, 2017
Technology
19
19k
PHPのエラーと例外再入門 / php-error-and-exception
PHPカンファレンス福岡2017の登壇資料です。
Hiraku NAKANO
June 10, 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
極める!メソッドチェーン #phpstudy / fluent-interface
hirak
8
2.7k
evalこそパワー #phpkansai / phpkansai2017
hirak
2
1.5k
メルカリ 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
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
180
ハイテク休憩
sat
PRO
2
180
サーバーなしでWordPress運用、できますよ。
sogaoh
PRO
0
120
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
2
270
TypeScript開発にモジュラーモノリスを持ち込む
sansantech
PRO
2
670
オプトインカメラ:UWB測位を応用したオプトイン型のカメラ計測
matthewlujp
0
200
Google Cloud で始める Cloud Run 〜AWSとの比較と実例デモで解説〜
risatube
PRO
0
120
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
39
16k
PHP ユーザのための OpenTelemetry 入門 / phpcon2024-opentelemetry
shin1x1
3
1.5k
Work as an App Engineer
lycorp_recruit_jp
0
370
OCI技術資料 : ファイル・ストレージ 概要
ocise
3
11k
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
180
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
67
4.5k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Visualization
eitanlees
146
15k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Done Done
chrislema
182
16k
Facilitating Awesome Meetings
lara
50
6.1k
Designing for humans not robots
tammielis
250
25k
Become a Pro
speakerdeck
PRO
26
5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Transcript
PHPͷ Τϥʔͱྫ֎࠶ೖ PHPΧϯϑΝϨϯεԬ 2017
ࣗݾհ • த (@Hiraku) • https://github.com/hirak • ීஈϝϧΧϦͰαʔόʔαΠυΤϯδχΞ ͬͯ·͢
• Composerؔ࿈ͷൃදΛΑ͍ͯ͘͠·͢
None
None
None
None
PHPͷ Τϥʔͱྫ֎࠶ೖ PHPΧϯϑΝϨϯεԬ 2017
ͳΜͰࠓ͞Β ʮΤϥʔͱྫ֎ʯʁ
None
ྫ֎͕Β͍͠ʂ • 2016ʙ ҰॠQiitaϒϩάͰྫ֎ͷهࣄ ͕૿͑ͨ • http://php-genba.shin1x1.com/1 • ͨͿΜݯPHPΧϯϑΝϨϯε2016Ͱͷ @t_wada
͞Μͷߨԋ
None
͔Ϳͬͨ… ʘ(^o^)ʗ
ྫ֎+PHPͷࣄ ͕ ηογϣϯͰ͢
࣍ 1. ʮΤϥʔʯͷఆٛ 2. ൃੜͱޙ࢝ͷ 3. PHPͷΤϥʔͱྫ֎ͷػೳ 4. ྫ֎҆શͱΦϒδΣΫτࢦ 5.
ϙέϞϯ
1ষ ʮΤϥʔʯͷఆٛ
ྫ: HTTPͰϑΝΠϧऔಘ͢Δؔ • $resp = http_get('http://php.net/') • ࣗલ࣮͢ΔͳΒɺͲΜͳΤϥʔॲཧΛॻ͖ ·͔͢ʁ
http_get(true); // boolean͕͞ΕͨΜͰ͚͢Ͳʂ http_get('uso800'); // URL͡Όͳ͍ʂ http_get('http://uso800.example.com/'); // !υϝΠϯݟ͔ͭΒͳ͍Α http_get('http://heavy.example.com/');
// !3࣌ؒͨͬͯऴΘΒͳ͍ΜͰ͕͢
Τϥʔͱ • Կ͔ྑ͘ͳ͍͜ͱ͕ى͖ͨ • ॲཧΛਐΊΒΕͳ͍ • ʮॲཧΛதࢭ͢Δʯ • ʮԡ͠௨͢ʯ
http_get(true); // boolean͕͞ΕͨΜͰ͚͢Ͳʂ http_get('uso800'); // URL͡Όͳ͍ʂ http_get('http://uso800.example.com/'); // !υϝΠϯݟ͔ͭΒͳ͍Α http_get('http://heavy.example.com/');
// !3࣌ؒͨͬͯऴΘΒͳ͍ΜͰ͕͢ ←ॻ͍͕ͨͭѱ͍ ʢόάʣ ←Β͍ࣄނ
୭͕ѱ͍͔ • ͍ํ͕ؒҧ͍ͬͯΔύλʔϯ • Ͳ͏ͯ͠ൃੜ͠͏Δύλʔϯ (࣮ߦ࣌Τϥʔ) • typoͳͷ͔ʁ ࣮ߦ࣌Τϥʔͳͷ͔ʁ •
͔ؔΒผͰ͖ͳ͍ύλʔϯ
None
PHPͷཱͰʮΤϥʔʯͱ • PHPࣗମ • ʮιʔείʔυΛड͚औͬͯʯ • ʮ࣮ߦ͢Δʯ • ؔΈ͍ͨͳͷ •
PHPʹͱͬͯԿ͔ྑ͘ͳ͍͜ͱ͕ى͖ͨ ʹΤϥʔ
PHPʹͱͬͯྑ͘ͳ͍͜ͱ • มͳιʔείʔυ͕͞Εͨ • ࣮ߦͯ͠ΈͨΒมͳ͜ͱ͕ى͖ͨ
php > "Hello world"Λग़ྗͯ͠Լ͍͞ɻ; PHP Parse error: syntax error, unexpected
identifier (T_STRING) in php shell code on line 1 ԿޠͩΑͦΕʂ ཧղෆೳɺॲཧࢭΊΔͶ
php > echo Hello; PHP Notice: Use of undefined constant
Hello - assumed 'Hello' in php shell code on line 1 Hello ະఆٛఆ… ͨͿΜ""ΛΕͯΔΜͩͳʂ উखʹจࣈྻѻ͍ʹͯ͠Ζ͏ ͨͩͪ͠ΐͬͱո͍͔͠Β Τϥʔͱͯ͠௨
php > echo [1,2,3]; PHP Notice: Array to string conversion
in php shell code on line 1 Array ྻΛecho… ༷ͱͯ͠"Array"ͱग़ྗ ͢Ε͍͍Μ͚ͩͲɺո͍͔͠Β Τϥʔͱͯ͠௨
PHP͞ΜେมͳͷͰ͢ • ίϯύΠϧɺͱ͍͏લॲཧ͕ͳ͍ • ͯ͢Λ࣮ߦ࣌ʹॲཧ͠ɺո͚͠Εͱʹ͔ ͘ΤϥʔΛు͘ • ଞͷݴޠͩͱlintίϯύΠϧ࣌warningʹ૬ ͢Δ෦ʮΤϥʔʯͷൣᙝʹͳΔ
PHPͷΤϥʔ৭ʑ • ߏจ͓͔͍͠ (parse error) • ܕએݴҧ • ͪΐͬͱ͋Γ͑ͳ͍҉ͷܕม •
ഇࢭ༧ఆͷؔΛ࣮ߦͨ͠ (deprecated) • ഇࢭ༧ఆͷߏจΛ͍ͬͯΔ (strict)
1ষ ·ͱΊ • PHPιʔείʔυΛͦͷͰ࣮ߦͰ͖ͳ͚ Ε͍͚ͳ͍ͷͰɺʮΤϥʔʯͷൣғ͕͍ • lintʹ͍ۙ༰ʮΤϥʔʯʹͳΔ
2ষ ൃੜͱޙ࢝ͷ
࠶ܝ: HTTPͰϑΝΠϧऔಘ͢Δؔ • $resp = http_get('http://php.net/') • ࣗલ࣮͢ΔͳΒɺͲΜͳΤϥʔॲཧΛॻ͖ ·͔͢ʁ
͠ɺhttp_getؔͷ༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒϦτϥΠ͠ଓ͚Δʯ ͩͬͨΒʁ • URLΛtypoͨ͠Β
͠ɺhttp_getؔͷ༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒۭจࣈΛฦ͢ʯ ͩͬͨΒʁ • ຊʹϨεϙϯε͕ۭจࣈͩͬͨͱ͖ͱݟ ͚͕͔ͭͳ͍
͠ɺhttp_getؔͷ༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒϩάʹهͯ͠falseΛ ฦ͢ʯͩͬͨΒʁ • ϩάͷॻ͖ࠐΈઌͬͯͲ͜Ͱઃఆ͢Δͷʁ
ͳʹ͔ؒҧ͍͑ͯΔͷͰ…
Τϥʔͷޙ࢝ͷಛ • ঢ়گʹΑͬͯมΘΓ͕ͪ • ϦτϥΠ • ϩάʹు͍ͯఘΊΔ (ϑΝΠϧ/ඪ४ग़ྗ) • ผͷؔʹϑΥʔϧόοΫ͢Δ
• etc
Τϥʔൃੜͷύλʔϯ • ҧ͏ؔͰಉ͡Τϥʔ͕ى͖Δ͜ͱසग़ • ྫ) http_get()ͱhttp_post()ϗετ໊͕ղܾ Ͱ͖ͳ͚Εಉ͡Τϥʔ • ͍͍ͪͪॲཧΛॻ͍ͯͨΒίϐϖͩΒ͚ʹͳ Δ
ൃੜͱޙ࢝Λ ͠·͠ΐ͏
͠ɺhttp_getؔͷ༷͕ • ʮऔಘͰ͖ͳ͔ͬͨΒΤϥʔΛ௨͢Δʯ(͋ ͑ͯԿॲཧ͠ͳ͍)ͩͬͨΒʁ • ঢ়گʹԠͯ͡ΤϥʔॲཧΛมߋͰ͖Δ✅ • ΤϥʔॲཧΛڞ௨ԽͰ͖Δ✅
ΤϥʔΛ௨͢ΔͨΊʹ • શͯͷॲཧதࢭ • ޙ࢝Ͱඞཁͳใͷίϐʔ͕ඞཁ • ʮԿ͕ى͖ͨͷ͔ʯ • ʮͲͷॲཧΛ͍ͯͨ͠࠷தͳͷ͔ʯ
Τϥʔͷ௨ํ๏͍Ζ͍Ζ • Τϥʔίʔυ(int)Λฦ͢ (Cݴޠͱ͔) • ίʔϧόοΫΛذ͢Δ(Promise) • ΤϥʔΛλϓϧͰฦ͢ (Goͱ͔) •
ྫ֎Λ͛Δ
ྫ֎Ҏ֎ͷྲྀͷ ͠·ͤΜ
ྫ֎ΛUISPX͢Δؔ ྫ֎ΛUISPX͢Δؔ ྫ֎ΛUISPX͢Δؔ ຊ൪ڥͰ DBUDI͢Δؔ ։ൃڥͰ DBUDI͢Δؔ Τϥʔ͕ى͖ͨΒ ྫ֎Λ͛Δ͚ͩ େଟͬͪ͜
ྫ֎Λੳͯ͠ దͳޙ࢝Λ͢Δ
͋ΔؔͷҰੜ ॲཧ։࢝ ਖ਼ৗऴྃ ؔݺͼग़͠ ؔݺͼग़͠
ྫ֎͕throw͞ΕͨΒ ॲཧ։࢝ try catch UISPXOFX 999&YDFQUJPO (͜ͷลͷॲཧ࣮ߦ͞Εͳ͍ catchʹॻ͍ͯ͋Δॲཧ
ྫ֎͕ͬͯ͘ΕΔ͜ͱ • ✅શͯͷॲཧதࢭ • ✅ޙ࢝ͰඞཁͳใΛ$eʹؚΊΒΕΔ • ✅͋ͱͰ·ͱΊͯtry ~ catch Ͱ͖Δ
2ষͷ·ͱΊ • ΤϥʔॲཧΛ៉ྷʹ͢Δʹɺൃੜͱޙ࢝ Λ͚ͯॻ͘ͱΑ͍ • ྫ֎͜ͷͨΊͷػೳ
3ষ PHPͷΤϥʔͱྫ֎
ͷ Τϥʔؔ ྺ࢙తܦҢͰෳࡶ
ʮΤϥʔʯ ͱ ʮྫ֎ʯ
PHPͷʮΤϥʔʯ • PHPΤϯδϯࣗମ͕ΤϥʔΛ௨͢Δͱ͖ʹ ͏ • ใจࣈྻͷΈ • ໌ࣔతʹൃੜͤ͞Δ͜ͱͰ͖Δ • trigger_error("ഇࢭ༧ఆ",
E_USER_DEPRECATED)
ΤϥʔϨϕϧʹΑͬͯಈ͖͕ҧ ͏ • E_ERROR ... ॲཧ͕தஅ͞ΕΔɻ΄΅෮ؼͰ͖ͳ͍ • E_WARNING, E_NOTICE ͦͷଞଟ
... ॲཧதஅ͞Εͳ͍
PHPͷʮྫ֎ʯ • PHP5͔Βಋೖ͞Εͨʮྫ֎ॲཧػߏʯ • ॊೈʹΩϟονͰ͖ͯແࢹͰ͖ͳ͍ • ྫ֎Ϋϥεࣗ༝ʹ֦ுͰ͖Δ • ͖ͳใΛ٧ΊͯOK
Τϥʔ ྫ֎ ੲ͔Β͋Δ 1)1Ҏ߱ ΤϥʔϝοηʔδͷΈؚΊΒΕΔ ԿͰؚΊΒΕΔɺ ΦϒδΣΫτ͝ͱ͛ΒΕΔ ΈࠐΈͷΤϥʔϨϕϧ͔Β બͿ͔͠ͳ͍ ͍͘ΒͰܕΛ૿ͤΔ
֦ுͰ͖Δ ઃఆ!ԋࢉࢠͰ ؆୯ʹແࢹͰ͖Δ ແࢹͰ͖ͳ͍ ѲΓͭͿ͢ίʔυॻ͚Δ
Ͳ͏ݟͯ ྫ֎ͷํ͕ߴػೳ
ΤϥʔΛྫ֎ʹม͢Δຐ๏ →Τϥʔෆཁͳͷʁ
ͦΜͳ͜ͱͳ͍Α ΤϥʔศརͳࢠͩΑ
Τϥʔͷॴ=ແࢹͰ͖Δ͜ͱ • ઃఆͰҰׅແࢹ or @ԋࢉࢠͰݸผʹແࢹ • ʮॲཧΛࢭΊΔඞཁͳ͍͕ɺϓϩάϥϚʹ Ұݴڭ͍͑ͨʯͱ͖ͬͪ͜ͷํ͕ศར • E_DEPRECATED͕දతͳྫ
ϥΠϒϥϦͷAPIΛมߋ͍ͨ͠ ͱ͖ function doSomething() { trigger_error( 'doSomethingͷΘΓʹdoSomething2ΛͬͯԼ͍͞', E_USER_DEPRECATED ); return
doSomething2(); }
deprecatedΛྫ֎ʹ͢Δͱ • ྫ֎ແࢹͰ͖ͳ͍ͷͰ… • ݹ͍ؔΛ͍ͬͯΔશ͕ͯյΕΔ • શ෦मਖ਼͠ͳ͍ͱҠߦͰ͖ͳ͍ • मਖ਼ͯ͠Δؒɺ৽ػೳ͕͑ͳ͍ʂ
deprecated͕ΤϥʔͳΒ • Ұ୴E_DEPRECATED, E_USER_DEPRECATED Λແࢹ͢Δઃఆʹͯ͠Ξοϓσʔτ • ։ൃڥͰ༗ޮʹͯ͠ɺൃੜΛ௵͍ͯ͘͠ • ͍ͩͿݮΒͤͨΒɺຊ൪Ͱ༗ޮʹͯ͠ɺൃ ੜͷ໓Λ֬ೝ͢Δ
ଞͷखஈ • /** @deprecated */ • ௨ৗར༻ऀ͕ಡΜͰ͘Εͳ͍ͱҙຯͳ͍ • error_log •
ϩά͋͘·ͰจࣈྻͰ͔͠ͳ͍
ݸਓతͳ͍͚ • ࣗͷίʔυͰྫ֎Λ͏ • େͷPHPΤϥʔɺErrorExceptionʹม͢Δ • ͨͿΜόάͳͷͰಠࣗʹcatch͠ͳ͍ • ͨͩ͠ɺE_DEPRECATEDܥͳͲྫ֎ʹ͠ͳ͍ ํ͕ศར
set_error_handlerୈೋҾ set_error_handler( function(...){ throw new ErrorException(...); }, E_ALL ^ E_DEPRECATED
^ E_USER_DEPRECATED ^ E_USER_NOTICE ); ϋϯυϦϯάͨ͘͠ͳ͍ ΤϥʔϨϕϧΛ আ֎͓ͯ͘͠
ΤϥʔϨϕϧ͍͚ • E_ERROR ... PHP7Ͱྫ֎ʹͳͬͨ • E_WARNING, E_NOTICE ... େɺΫιίʔυ͕Ҿ͖ى͜͠
͍ͯΔͷͰྫ֎ԽɻଈࠁࢭΊͯσόοά • E_STRICT, E_DEPRECATED ... ແࢹͯ͠ಈ͘ͷͰྫ֎Խ͠ ͳ͍ɻඞཁʹԠͯ͡ࢹɺσόοά • E_USER_XXXX .. ͋͑ͯΤϥʔΛ͍ͬͯΔྫͳͷͰɺྫ֎ ʹ͠ͳ͍͍͔ͯ͘ɻ
3ষ·ͱΊ • PHPʹʮΤϥʔʯͱʮྫ֎ʯ͕͋Δ • Τϥʔ؆୯ͳ௨ͱͯ͠ศར • ྫ֎ແࢹͰ͖ͳ͍ • جຊతʹྫ֎Λ͓͏
4ষ ྫ֎҆શͱΦϒδΣΫτࢦ
ྫ֎ͷϓϥε໘ • ίʔυதʹΤϥʔॲཧ͕ొ͠ͳ͍ͷͰɺ ៉ྷʹͳΔ • ʮίʔυఀࢭʯΛࣗಈͰͬͯ͘ΕΔ • ແࢹ͢ΔͱΫϥογϡ͢ΔͷͰແࢹͰ͖ͳ͍ɻ ΤϥʔॲཧΛ͖ͬͪΓߦ͏ίʔυΛཁٻ͢Δɻ
ྫ֎ͷͳ͍ੈք $ret = doSomething(); if ($ret === false) { //
Τϥʔॲཧ͍ͬͺ͍ return false; } $ret2 = doSomething2(); if ($ret2 === false) { // Τϥʔॲཧ͍ͬͺ͍ return false; }
ྫ֎ͷ͋Δੈք $ret = doSomething(); $ret2 = doSomething2(); // ... Ͳ͔͜ݺͼग़͠ͷ
্ྲྀͷํͰҰ͚ͩ try { } catch { } ͯ͋͠ΕOK
࠶ܝɿ ྫ֎͕throw͞ΕͨΒ ॲཧ։࢝ try catch (͜ͷลͷॲཧ࣮ߦ͞Εͳ͍ catchʹॻ͍ͯ͋Δॲཧ UISPXOFX 999&YDFQUJPO
͓ؾ͖ͮͩΖ͏͔…
ྫ֎͕throw͞ΕͨΒ ॲཧ։࢝ try UISPXOFX 999&YDFQUJPO catch (͜ͷลͷॲཧ࣮ߦ͞Εͳ͍ த్ͳͱ͜ΖͰࢭ·͚ͬͨͲɺ ຊʹେৎͳͷ͔ʁ catchʹॻ͍ͯ͋Δॲཧ
ྫ֎ͷϚΠφε໘ • ͍ͭͲ͜Ͱॲཧ͕தஅ͞ΕΔ͔Θ͔Βͳ͍ • Ͳ͜Ͱதஅ͞ΕͯΞϓϦέʔγϣϯ͕յΕͳ ͍Α͏ʹؾΛ͚ͭΔඞཁ͕͋Δ • յΕͳ͍ੑ࣭Λʮྫ֎҆શʯͱݴ͏ • ྫ֎Λѻ͏ਓɺ
ৗʹҙࣝ͠ͳ͚ΕͳΒͳ͍
None
ʮྫ֎҆શʯΛάάΔͱ • C++ͷ͕ଟ͍ • ྫ֎҆શҧ=ଈࢮͷੈք • ͨͩɺݴޠؔͳ͘ॏཁͰ͢ʂ • ྫΛڍ͛·͢
ྫ1
ِͷτϥϯβΫγϣϯ $db->insert($a); doSomething(); $db->insert($b); ... ͜͜Ͱྫ֎͕ى͖ͨΒ த్ͳϨίʔυ͕Ͱ͖ͪΌ͏
DBૢ࡞தͷྫ֎ • DBૢ࡞ͷ݁Ռ͕ͬͯ͠·͏ • ෆ۩߹ͷݪҼʹͳΔ • ૢ࡞͕ਖ਼ৗʹऴΘ͔ͬͨɺԿͳ͔͔ͬͨɺ ͲͪΒ͔ʹ͢Δ = DBͷτϥϯβΫγϣϯػೳ
Λ͏
[ྫ֎҆શ]τϥϯβΫγϣϯ $db->beginTransaction(); try { $db->insert($a); doSomething(); $db->insert($b); $db->commit(); } catch
(\Exception $e) { $db->rollback(); throw $e; }
ྫ2
κϯϏΩϟογϡ if (self::$cache) return self::$cache; $result = doSomething1(); self::$cache =
$result; doSomething2($result); ͜͜Ͱྫ֎͕ى͖ͨΒʁ Ωϟογϡͬͯ͠·͏͚Ͳʁ
ण໋ͷ͍มͷѻ͍ʹҙ • Ϋϥεͷstaticมɺάϩʔόϧม • ྫ֎Ͱதஅ͞Εͯੜ͖ͬͨ·· • ྫ֎ൃੜΛτϥοϓͯ͠আ͢Δඞཁ͕͋Δ ͔
ྫ3
Cݴޠ෩ͷؔ ob_start(); doSomething(); $output = ob_get_clean(); ͜͜Ͱྫ֎͕ى͖ͨΒʁ PC@HFU@DMFBO ݺΕͳ͘Ͷʁ
ͦͦΈࠐΈͷ͕ؔ ྫ֎҆શ͡Όͳ͍ • ob_start / ob_clean • fopen / fclose
• ͕ؔରʹͳ͍ͬͯΔྫ͕݁ߏ͋Δ
PHPࣗମ͕कͬͯ͘ΕΔ҆શੑ • ؔϩʔΧϧͳมGC͕ճऩͯ͘͠ΕΔ • fopenͳͲͷϦιʔεܕࣗಈͰด͡ΒΕΔ • ͔͠͠ʮstaticมʯʮ֎෦γεςϜʯʮDBʯ ͳͲ͜ͷൣғʹؚ·Εͳ͍ • ѻ͏ਓ͕ྫ֎҆શΛҙࣝ͢Δ͔͠ͳ͍
ྫ֎҆શͷ͠͞ • ྫ֎҆શ͔Ͳ͏͔ػցతʹஅͮ͠Β͍ • ίʔυʹؔΘΔਓશһ͕कΔඞཁ͕͋Δ • ͜Ε͕͗͢͠Δ
ྫ֎҆શΛ࣮ݱ͢Δ ΠσΟΦϜू
RAII (scope & destructor) class Curl { function __construct() {
$this->ch = curl_init(); } function __destruct() { curl_close($this->ch); } }
RAII (scope & destructor) function req() { $ch = new
Curl(); //... //... } Ҏ߱ɺͲ͜Ͱॲཧ͕ தஅͯ͠σετϥΫλ͕ ޙ࢝ͯ͘͠ΕΔͷͰฏؾ
RAIIͷϋϚΓͲ͜Ζ • େ͏·͍͘͘ͷ͕ͩɺExceptionͷελο ΫτϨʔεͳͲʹั·Δͱɺण໋͕৳ͼΔ • ࢥͬͨλΠϛϯάͰclose͞Εͳ͘ͳΔ͔
ൣғͰcatchͯ͠re-throw try { ... } catch (Exception $e) { ...
//ޙ࢝Λॻ͘ throw $e; }
࠷ॳ͔ΒfinallyͰॻ͘ try { $ch = curl_init(); //... } finally {
curl_close($ch); }
ྫ֎ͱਖ਼͖͘͠߹͏ ͷ͍͠
͔͠͠ ʮྫ֎ࣗମΛΘͳ͍ʯ ͷ͍͠
4ষ ྫ֎҆શͱΦϒδΣΫτࢦ
ྫ֎ͱΦϒδΣΫτࢦ • ຊདྷɺྫ֎ΦϒδΣΫτࢦͱશؔ͘ͷͳ ͍֓೦ • PHPExceptionΫϥεΛܧঝͨ͠ΦϒδΣΫτ ͔͠throwͰ͖ͳ͍͚Ͳ • 400 ͱ͔
"Τϥʔ͕ى͖·ͨ͠" ͱ͔Λthrow Ͱ͖Δݴޠ͋Δ
ΦϒδΣΫτࢦͯ͠Δͱ • ίϯετϥΫλΛଟ༻ͨ͘͠ͳΔͣ • த్ͳঢ়ଶ͕ͳ͍͜ͱ͕อূͰ͖Δ • ίϯετϥΫλͦͷ্༷ɺ Τϥʔ௨ʹྫ֎͔͑͠ͳ͍ • return
false ͕Ͱ͖ͳ͍͔ΒͶ
ྫ) PDO • σϑΥϧτͩͱྫ֎Λు͔ͳ͍ઃఆ • $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); • ͔͠͠ɺnew PDO͢Δͱ͖ͷΤϥʔɺઃఆ
ʹؔͳ͘ৗʹ1%0&YDFQUJPO
࣮ࡍʹ࣮ߦͨ͠ྫ 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
ͦͷଞͷࣄ • PHP7ͰE_ERROR͕ྫ֎ͷΑ͏ͳ;Δ·͍Λ ͢ΔΑ͏ʹͳͬͨ • ݴޠΈࠐΈػೳঃʑʹΤϥʔ͔Βྫ֎ʹ ஔ͖͍͑ͯͧ͘એݴ͕ग़͍ͯΔ • ͏ɺྫ֎ͷൃੜ͠ͳ͍ੈքͳͲPHPʹ ͳ͍
զʑྫ֎ͱ ڞʹੜ͖Δ͔͠ͳ͍ͷͰ͢
4ষ·ͱΊ • PHPerྫ֎҆શΛҙࣝ͠·͠ΐ͏ • ΫϥεΛ͑ྫ֎Λ͏Ҏ֎બࢶ͕ͳ͍ ͷͰɺݡ͖͘߹͍͖ͬͯ·͠ΐ͏
5ষ ϙέϞϯ
try { doSomething(); ... } catch (Exception $e) { //
do nothing }
Pokémon catching • ͨΒ͍ྫ֎ΫϥεͰcatch͍ͯ͠Δ͜ͱͷ ଏশ • RuntimeException/Exception/Throwable • (ϙέϞϯͷΩϟονϑϨʔζʮCatch 'em
allʯ͔Β࿈ͨ͠ͱࢥΘΕΔʣ
Pokémon catchingΛ ͍͚ͬͯͳ͍
try { doSomething(); ... } catch (Exception $e) { //
do nothing }
ԿΛั·͍͑ͨͷ…ʁ • ྫ֎͍ΖΜͳؔͰൃੜ͢ΔՄೳੑ͕͋Δ • PHPࣗͷΤϥʔ͔͠Εͳ͍ • ͋ͳͨຊʹશͯΛదʹޙॲཧͯ͠Δʁ • ͦ͏Ͱͳ͚Εɺࣗͷҙਤͨ͠ྫ֎ͷΈʹ ߜ͓ͬͯ͘
set_error_handlerͱͷ Έ߹Θͤ • PHPΤϥʔΛErrorExceptionʹม͢Δ͜ͱ ͕Ͱ͖Δ • Pokémon catching͕Έ߹Θ͞Δͱࠈ
None
ྫ֎ॲཧʹר͖ࠐ·Εͯtypo ͷҐஔ͕Θ͔Βͳ͍…ʂ
ɾPokémon catching • ྫ֎ࣗͷͨΊʹ༻ҙ͢Δ & ͦΕͷΈΛ catch͢Δ • class HogeException
extends \RuntimeException • setterΛ૿͠·͠ΐ͏
Guzzleͷྫ
ޙ࢝͢ΔਓͷͨΊʹ • ඞཁͦ͏ͳΛExceptionΫϥεʹ٧ΊΒΕΔ Α͏ʹ͢Δ • throw (new HogeException($message, $code)) ->setFoo($foo)
->setMoo($moo);
ͱ͍͑ɺຊ൪Ͱcatch͞Εͳ ͍ྫ֎͕ى͖ͨΒϠόΠ…ʂ
None
อݥͷset_exception_handler • catch͞Εͳ͔ͬͨྫ֎Λั·͑Δ࠷ޙͷࡆ • େͨ͜͠ͱग़དྷͳ͍ͷͰɺϩΪϯάͯ͠Ϣʔ βʔʹΤϥʔը໘ΛݟͤΔ͚ͩɺͱ͔ • ຊ൪ͷΈಈ͔͢ɺͳΜͯ͜ͱՄೳ
͜Ε͑͋͞Εྫ֎ಥ͖ൈ͚ ͳ͍ set_exception_handler( function (Throwable $e) { echo 'Τϥʔ͕ى͖·ͨ͠'; exit;
} );
શମͷ·ͱΊ
Τϥʔͱྫ֎ͷ࠶ೖ • PHPͷΤϥʔແࢹͰ͖Δ͜ͱʹՁ͕͋Γ· ͢ɻdeprecatedͱͯ͠ͷ͍ํΦεεϝ • ྫ֎ศརͰ͕͢ɺྫ֎҆શΛҙࣝ͠ͳ͚Ε͍ ͚ͳ͍͜ͱPHPͰมΘΓ·ͤΜ • ExceptionThrowableΛอݥతҎ֎Ͱcatch͠ ͳ͍Α͏ʹ͠·͠ΐ͏
We are hiring!