Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

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

Hiraku NAKANO

June 10, 2017
Tweet

More Decks by Hiraku NAKANO

Other Decks in Technology

Transcript

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

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

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

    in php shell code on line 1 Array ഑ྻΛecho…
 ࢓༷ͱͯ͠͸"Array"ͱग़ྗ
 ͢Ε͹͍͍Μ͚ͩͲɺո͍͔͠Β Τϥʔͱͯ͠௨஌
  4. PHPͷΤϥʔ৭ʑ • ߏจ͓͔͍͠ (parse error) • ܕએݴҧ൓ • ͪΐͬͱ͋Γ͑ͳ͍҉໧ͷܕม׵ •

    ഇࢭ༧ఆͷؔ਺Λ࣮ߦͨ͠ (deprecated) • ഇࢭ༧ఆͷߏจΛ࢖͍ͬͯΔ (strict)
  5. set_error_handlerୈೋҾ਺ set_error_handler( function(...){ throw new ErrorException(...); }, E_ALL ^ E_DEPRECATED

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

    ͍ͯΔͷͰྫ֎ԽɻଈࠁࢭΊͯσόοά • E_STRICT, E_DEPRECATED ... ແࢹͯ͠΋ಈ͘ͷͰྫ֎Խ͠ ͳ͍ɻඞཁʹԠͯ͡؂ࢹɺσόοά • E_USER_XXXX .. ͋͑ͯΤϥʔΛ࢖͍ͬͯΔྫͳͷͰɺྫ֎ ʹ͠ͳ͍͍͔ͯ͘΋ɻ
  7. ྫ֎ͷͳ͍ੈք $ret = doSomething(); if ($ret === false) { //

    Τϥʔॲཧ͍ͬͺ͍ return false; } $ret2 = doSomething2(); if ($ret2 === false) { // Τϥʔॲཧ͍ͬͺ͍ return false; }
  8. κϯϏΩϟογϡ if (self::$cache) return self::$cache; $result = doSomething1(); self::$cache =

    $result; doSomething2($result); ͜͜Ͱྫ֎͕ى͖ͨΒʁ Ωϟογϡ࢒ͬͯ͠·͏͚Ͳʁ
  9. RAII (scope & destructor) class Curl { function __construct() {

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

    Curl(); //... //... } Ҏ߱ɺͲ͜Ͱॲཧ͕
 தஅͯ͠΋σετϥΫλ͕
 ޙ࢝຤ͯ͘͠ΕΔͷͰฏؾ
  11. ࣮ࡍʹ࣮ߦͨ͠ྫ 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