PHPのエラーと例外再入門 / php-error-and-exception

PHPのエラーと例外再入門 / php-error-and-exception

PHPカンファレンス福岡2017の登壇資料です。

2295fec4d38f59366f983d38628e6ca0?s=128

Hiraku NAKANO

June 10, 2017
Tweet

Transcript

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

  2. ࣗݾ঺հ • த໺ ୓ (@Hiraku) • https://github.com/hirak • ීஈ͸ϝϧΧϦͰαʔόʔαΠυΤϯδχΞ
 ΍ͬͯ·͢

    • Composerؔ࿈ͷൃදΛΑ͍ͯ͘͠·͢
  3. None
  4. None
  5. None
  6. None
  7. PHPͷ
 Τϥʔͱྫ֎࠶ೖ໳ PHPΧϯϑΝϨϯε෱Ԭ 2017

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

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

    ͞Μͷߨԋ
  11. None
  12. ͔Ϳͬͨ… ʘ(^o^)ʗ

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

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

    ϙέϞϯ໰୊
  15. 1ষ ʮΤϥʔʯͷఆٛ

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

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

    // !3࣌ؒͨͬͯ΋ऴΘΒͳ͍ΜͰ͕͢
  18. Τϥʔͱ͸ • Կ͔ྑ͘ͳ͍͜ͱ͕ى͖ͨ • ΋͸΍ॲཧΛਐΊΒΕͳ͍ • ʮॲཧΛதࢭ͢Δʯ • ʮԡ͠௨͢ʯ

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

    // !3࣌ؒͨͬͯ΋ऴΘΒͳ͍ΜͰ͕͢ ←ॻ͍ͨ΍͕ͭѱ͍ ʢόάʣ ←΋Β͍ࣄނ
  20. ୭͕ѱ͍͔ • ࢖͍ํ͕ؒҧ͍ͬͯΔύλʔϯ • Ͳ͏ͯ͠΋ൃੜ͠͏Δύλʔϯ
 (࣮ߦ࣌Τϥʔ) • typoͳͷ͔ʁ ࣮ߦ࣌Τϥʔͳͷ͔ʁ •

    ؔ਺͔Β͸൑ผͰ͖ͳ͍ύλʔϯ
  21. None
  22. PHPͷཱ৔ͰʮΤϥʔʯͱ͸ • PHPࣗମ΋ • ʮιʔείʔυΛड͚औͬͯʯ • ʮ࣮ߦ͢Δʯ • ؔ਺Έ͍ͨͳ΋ͷ •

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

  24. php > "Hello world"Λग़ྗͯ͠Լ͍͞ɻ; PHP Parse error: syntax error, unexpected

    identifier (T_STRING) in php shell code on line 1 ԿޠͩΑͦΕʂ ཧղෆೳɺॲཧࢭΊΔͶ
  25. php > echo Hello; PHP Notice: Use of undefined constant

    Hello - assumed 'Hello' in php shell code on line 1 Hello ະఆٛఆ਺…
 ͨͿΜ""Λ๨ΕͯΔΜͩͳʂ উखʹจࣈྻѻ͍ʹͯ͠΍Ζ͏
 ͨͩͪ͠ΐͬͱո͍͔͠Β Τϥʔͱͯ͠௨஌
  26. php > echo [1,2,3]; PHP Notice: Array to string conversion

    in php shell code on line 1 Array ഑ྻΛecho…
 ࢓༷ͱͯ͠͸"Array"ͱग़ྗ
 ͢Ε͹͍͍Μ͚ͩͲɺո͍͔͠Β Τϥʔͱͯ͠௨஌
  27. PHP͞Μ͸େมͳͷͰ͢ • ίϯύΠϧɺͱ͍͏લॲཧ͕ͳ͍ • ͢΂ͯΛ࣮ߦ࣌ʹॲཧ͠ɺո͚͠Ε͹ͱʹ͔ ͘ΤϥʔΛు͘ • ଞͷݴޠͩͱlint΍ίϯύΠϧ࣌warningʹ૬ ౰͢Δ෦෼΋ʮΤϥʔʯͷൣᙝʹͳΔ

  28. PHPͷΤϥʔ৭ʑ • ߏจ͓͔͍͠ (parse error) • ܕએݴҧ൓ • ͪΐͬͱ͋Γ͑ͳ͍҉໧ͷܕม׵ •

    ഇࢭ༧ఆͷؔ਺Λ࣮ߦͨ͠ (deprecated) • ഇࢭ༧ఆͷߏจΛ࢖͍ͬͯΔ (strict)
  29. 1ষ ·ͱΊ • PHP͸ιʔείʔυΛͦͷ৔Ͱ࣮ߦͰ͖ͳ͚ Ε͹͍͚ͳ͍ͷͰɺʮΤϥʔʯͷൣғ͕޿͍ • lintʹ͍ۙ಺༰΋ʮΤϥʔʯʹͳΔ

  30. 2ষ ൃੜͱޙ࢝຤ͷ෼཭

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

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

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

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

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

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

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

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

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

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

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

    ྫ֎Λ౤͛Δ
  42. ྫ֎Ҏ֎ͷྲྀ೿ͷ
 ࿩͸͠·ͤΜ

  43. ྫ֎ΛUISPX͢Δؔ਺ ྫ֎ΛUISPX͢Δؔ਺ ྫ֎ΛUISPX͢Δؔ਺ ຊ൪؀ڥͰ DBUDI͢Δؔ਺ ։ൃ؀ڥͰ DBUDI͢Δؔ਺ Τϥʔ͕ى͖ͨΒ
 ྫ֎Λ౤͛Δ͚ͩ େଟ਺͸ͬͪ͜

    ྫ֎Λ෼ੳͯ͠
 ద੾ͳޙ࢝຤Λ͢Δ
  44. ͋Δؔ਺ͷҰੜ ॲཧ։࢝ ਖ਼ৗऴྃ ؔ਺ݺͼग़͠ ؔ਺ݺͼग़͠

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

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

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

  48. 3ষ PHPͷΤϥʔͱྫ֎

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

  50. ʮΤϥʔʯ
 ͱ ʮྫ֎ʯ

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

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


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

  54. Τϥʔ ྫ֎ ੲ͔Β͋Δ 1)1Ҏ߱ ΤϥʔϝοηʔδͷΈؚΊΒΕΔ ԿͰ΋ؚΊΒΕΔɺ
 ΦϒδΣΫτ͝ͱ౤͛ΒΕΔ ૊ΈࠐΈͷΤϥʔϨϕϧ͔Β
 બͿ͔͠ͳ͍ ͍͘ΒͰ΋ܕΛ૿΍ͤΔ


    ֦ுͰ͖Δ ઃఆ΍!ԋࢉࢠͰ
 ؆୯ʹແࢹͰ͖Δ ແࢹͰ͖ͳ͍
 ѲΓͭͿ͢ίʔυ͸ॻ͚Δ
  55. Ͳ͏ݟͯ΋ ྫ֎ͷํ͕ߴػೳ

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

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

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

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

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

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

  62. ଞͷखஈ • /** @deprecated */ • ௨ৗ͸ར༻ऀ͕ಡΜͰ͘Εͳ͍ͱҙຯͳ͍ • error_log •

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

  64. set_error_handlerୈೋҾ਺ set_error_handler( function(...){ throw new ErrorException(...); }, E_ALL ^ E_DEPRECATED

    ^ E_USER_DEPRECATED ^ E_USER_NOTICE ); ϋϯυϦϯάͨ͘͠ͳ͍ ΤϥʔϨϕϧΛ আ֎͓ͯ͘͠
  65. ΤϥʔϨϕϧ࢖͍෼͚ • E_ERROR ... PHP7Ͱྫ֎ʹͳͬͨ • E_WARNING, E_NOTICE ... େ఍ɺΫιίʔυ͕Ҿ͖ى͜͠

    ͍ͯΔͷͰྫ֎ԽɻଈࠁࢭΊͯσόοά • E_STRICT, E_DEPRECATED ... ແࢹͯ͠΋ಈ͘ͷͰྫ֎Խ͠ ͳ͍ɻඞཁʹԠͯ͡؂ࢹɺσόοά • E_USER_XXXX .. ͋͑ͯΤϥʔΛ࢖͍ͬͯΔྫͳͷͰɺྫ֎ ʹ͠ͳ͍͍͔ͯ͘΋ɻ
  66. 3ষ·ͱΊ • PHPʹ͸ʮΤϥʔʯͱʮྫ֎ʯ͕͋Δ • Τϥʔ͸؆୯ͳ௨஌ͱͯ͠ศར • ྫ֎͸ແࢹͰ͖ͳ͍ • جຊతʹ͸ྫ֎Λ࢖͓͏

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

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

  69. ྫ֎ͷͳ͍ੈք $ret = doSomething(); if ($ret === false) { //

    Τϥʔॲཧ͍ͬͺ͍ return false; } $ret2 = doSomething2(); if ($ret2 === false) { // Τϥʔॲཧ͍ͬͺ͍ return false; }
  70. ྫ֎ͷ͋Δੈք $ret = doSomething(); $ret2 = doSomething2(); // ... Ͳ͔͜ݺͼग़͠ͷ


    ্ྲྀͷํͰҰ౓͚ͩ
 try { } catch { } ͯ͋͠Ε͹OK
  71. ࠶ܝɿ ྫ֎͕throw͞ΕͨΒ ॲཧ։࢝ try catch (͜ͷลͷॲཧ͸࣮ߦ͞Εͳ͍ catchʹॻ͍ͯ͋Δॲཧ΁ UISPXOFX 999&YDFQUJPO

  72. ͓ؾ͖ͮͩΖ͏͔…

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

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


    ৗʹҙࣝ͠ͳ͚Ε͹ͳΒͳ͍
  75. None
  76. ʮྫ֎҆શʯΛάάΔͱ • C++ͷ࿩͕ଟ͍ • ྫ֎҆શҧ൓=ଈࢮͷੈք • ͨͩɺݴޠؔ܎ͳ͘ॏཁͰ͢ʂ • ྫΛڍ͛·͢

  77. ྫ1

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

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

    Λ࢖͏
  80. [ྫ֎҆શ]τϥϯβΫγϣϯ $db->beginTransaction(); try { $db->insert($a); doSomething(); $db->insert($b); $db->commit(); } catch

    (\Exception $e) { $db->rollback(); throw $e; }
  81. ྫ2

  82. κϯϏΩϟογϡ if (self::$cache) return self::$cache; $result = doSomething1(); self::$cache =

    $result; doSomething2($result); ͜͜Ͱྫ֎͕ى͖ͨΒʁ Ωϟογϡ࢒ͬͯ͠·͏͚Ͳʁ
  83. ण໋ͷ௕͍ม਺ͷѻ͍ʹ஫ҙ • Ϋϥεͷstaticม਺ɺάϩʔόϧม਺ • ྫ֎Ͱதஅ͞Εͯ΋ੜ͖࢒ͬͨ·· • ྫ֎ൃੜΛτϥοϓͯ͠࡟আ͢Δඞཁ͕͋Δ ͔΋

  84. ྫ3

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

  86. ͦ΋ͦ΋૊ΈࠐΈͷؔ਺͕ ྫ֎҆શ͡Όͳ͍ • ob_start / ob_clean • fopen / fclose

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

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

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

  90. RAII (scope & destructor) class Curl { function __construct() {

    $this->ch = curl_init(); } function __destruct() { curl_close($this->ch);
 } }
  91. RAII (scope & destructor) function req() { $ch = new

    Curl(); //... //... } Ҏ߱ɺͲ͜Ͱॲཧ͕
 தஅͯ͠΋σετϥΫλ͕
 ޙ࢝຤ͯ͘͠ΕΔͷͰฏؾ
  92. RAIIͷϋϚΓͲ͜Ζ • େ఍͸͏·͍͘͘ͷ͕ͩɺExceptionͷελο ΫτϨʔεͳͲʹั·Δͱɺण໋͕৳ͼΔ • ࢥͬͨλΠϛϯάͰclose͞Εͳ͘ͳΔ͔΋

  93. ޿ൣғͰcatchͯ͠re-throw try { ... } catch (Exception $e) { ...

    //ޙ࢝຤Λॻ͘ throw $e; }
  94. ࠷ॳ͔ΒfinallyͰॻ͘ try { $ch = curl_init(); //... } finally {

    curl_close($ch); }
  95. ྫ֎ͱਖ਼͘͠෇͖߹͏ ͷ͸೉͍͠

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

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

  98. ྫ֎ͱΦϒδΣΫτࢦ޲ • ຊདྷɺྫ֎͸ΦϒδΣΫτࢦ޲ͱશؔ͘܎ͷͳ ͍֓೦ • PHP͸ExceptionΫϥεΛܧঝͨ͠ΦϒδΣΫτ ͔͠throwͰ͖ͳ͍͚Ͳ • 400 ͱ͔

    "Τϥʔ͕ى͖·ͨ͠" ͱ͔Λ௚઀throw Ͱ͖Δݴޠ΋͋Δ
  99. ΦϒδΣΫτࢦ޲ͯ͠Δͱ • ίϯετϥΫλΛଟ༻ͨ͘͠ͳΔ͸ͣ • த్൒୺ͳঢ়ଶ͕ͳ͍͜ͱ͕อূͰ͖Δ • ίϯετϥΫλ͸ͦͷ࢓্༷ɺ
 Τϥʔ௨஌ʹྫ֎͔͠࢖͑ͳ͍ • return

    false ͕Ͱ͖ͳ͍͔ΒͶ
  100. ྫ) PDO • σϑΥϧτͩͱྫ֎Λు͔ͳ͍ઃఆ • $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); • ͔͠͠ɺnew PDO͢Δͱ͖ͷΤϥʔ͸ɺઃఆ

    ʹؔ܎ͳ͘ৗʹ1%0&YDFQUJPO
  101. ࣮ࡍʹ࣮ߦͨ͠ྫ 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
  102. ͦͷଞͷࣄ৘ • PHP7ͰE_ERROR͕ྫ֎ͷΑ͏ͳ;Δ·͍Λ ͢ΔΑ͏ʹͳͬͨ • ݴޠ૊ΈࠐΈػೳ΋ঃʑʹΤϥʔ͔Βྫ֎ʹ ஔ͖׵͍͑ͯͧ͘એݴ͕ग़͍ͯΔ • ΋͏ɺྫ֎ͷൃੜ͠ͳ͍ੈքͳͲPHPʹ͸ ͳ͍

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

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

  105. 5ষ ϙέϞϯ໰୊

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

    do nothing }
  107. Pokémon catching • ΍ͨΒ޿͍ྫ֎ΫϥεͰcatch͍ͯ͠Δ͜ͱͷ ଏশ • RuntimeException/Exception/Throwable • (ϙέϞϯͷΩϟονϑϨʔζʮCatch 'em

    allʯ͔Β࿈૝ͨ͠ͱࢥΘΕΔʣ
  108. Pokémon catchingΛ ΍ͬͯ͸͍͚ͳ͍

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

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

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

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

  114. ୤ɾPokémon catching • ྫ֎͸ࣗ෼ͷͨΊʹ༻ҙ͢Δ & ͦΕͷΈΛ catch͢Δ • class HogeException

    extends \RuntimeException • setterΛ૿΍͠·͠ΐ͏
  115. Guzzleͷྫ

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


    ->setMoo($moo);
  117. ͱ͸͍͑ɺຊ൪Ͱcatch͞Εͳ ͍ྫ֎͕ى͖ͨΒϠόΠ…ʂ

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

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

    } );
  121. શମͷ·ͱΊ

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

  123. We are hiring!