PHPカンファレンス沖縄2019の懇親会LTで話したPHPの独自例外、とくに独自基底例外クラスについてアイデアです
© - BASE, Inc.A way to design user-defined exceptionin PHP〜PHPにおける独⾃例外設計を考える〜PHPカンファレンス沖縄 2019. . - @hgsgtk
View Slide
© - BASE, Inc.HAISAI OKINAWA!!!
© - BASE, Inc.Talk Abstract• PHP ( >=)における独⾃例外設計の考え⽅• How to design user-defined exception with PHP ( >=)• 特にアプリケーション領域の基底クラスの設計• Especially, a base exception class of application domain• Symfonyなどのフレームワークの内部実装も眺めて参考にする• Refer to internal implementations of some application frameworks
© - BASE, Inc.PHP >=sorry
© - BASE, Inc.var_dump($hgsgtk);: @hgsgtkKazuki HigashiguchiBASE BANK, Inc. / Dev Division / Tech LeadBackend Engineer
© - BASE, Inc.++ in Okinawahttps://speakerdeck.com/hgsgtk/design-not-to-broke-additional-windowhttps://speakerdeck.com/hgsgtk/understanding-design-pattern-from-the-origin10.11 前夜祭 Session 10.12 本編 LT
© - BASE, Inc.Motivation of error handling by exception• 適切なスコープをきってハンドリングしたい• I want to handle errors with appropriate scopes• 実⾏時エラー‧検査時エラー• Runtime error / Inspection error• ⾃分たちのチームコードのドメイン内‧外• Inside and outside of our development code
PHP Exception classes• Throwable: the baseinterface• Error and Exceptionclass implementsThrowable interface.© - BASE, Inc.https://www.php.net/manual/en/class.throwable.php
© - BASE, Inc.正当性と堅牢性 (Legitimacy and Robustness)正当性: システムの仕様、設計、実装の不具合のなさRobustness: No faults in system specifications, design and implementation堅牢性: 予期せぬ状況下でも機能し続けられる能⼒Robustness: Ability to continue to function under unexpectedcircumstancesSteve McConnell. Code Complete 第2版 完全なプログラミングを⽬指して / 第5部 コードの改良
© - BASE, Inc.正当性と堅牢性 (Legitimacy and Robustness)“正当性にこだわると堅牢性が損なわれるし、堅牢性にこだわると正当性が損なわれる。”“Sticking to legitimacy impairs robustness, andsticking to legitimacy impairs legitimacy.”Steve McConnell. Code Complete 第2版 完全なプログラミングを⽬指して / 第5部 コードの改良
© - BASE, Inc.Legitimate in the micro, robustness in the macrohttps://speakerdeck.com/twada/php-conference- ?slide=PHP で堅牢なコードを書く - 例外処理、表明プログラミング、契約による設計 / PHP Conference by Takuya Wada“個々のクラスは正当性を重視し堅牢性はアーキテクチャ/フレームワーク等で保証するのがオススメ”“It is recommended that each classemphasizes legitimacy and robustness isguaranteed by architecture or framework etc”
© - BASE, Inc.考えられる独⾃例外の⽅針 (A exception design policy)• 適切なスコープでのエラーハンドリングを促す• Encourage error handling with appropriate scope• 堅牢性をもったエラーハンドリングするための、チームコードの⼤スコープを補⾜できる• Complement the large scope of team code for robust error handling
© - BASE, Inc.ex: Symfony Component user-defined exceptionhttps://github.com/symfony/symfony/blob/cd ccf a d f ed c fd a c fd d /src/Symfony/Component/Validator/Exception/InvalidArgumentException.php#L
© - BASE, Inc.https://github.com/symfony/symfony/blob/ c fa ff fd d c fca f ac/src/Symfony/Component/Validator/Exception/ExceptionInterface.php#Lex: Symfony Component user-defined exception
© - BASE, Inc.• Componentのスコープで、Throwableを継承した独⾃インタフェースを作成している• Create a user-defined interface that inherits Throwable in the scope ofComponent• ⇒⾃分たちで作る例外全体を補⾜できる• -> Supplement the entire exception you create✍ Symfony Component user-defined exception
© - BASE, Inc.• 独⾃例外はPHP標準の例外クラスを継承しつつ、独⾃インタフェースを実装する構成• User-defined exception inherits PHP standard exception classes andimplement user-defined interface• ⇒独⾃例外をそれぞれの要件に合わせた適切な粒度へ• -> create exception classes proper granularity tailored to yourrequirements✍ Symfony Component user-defined exception
© - BASE, Inc.A idea to make your user-defined exception
© - BASE, Inc.A Base exception interface implementsthrowable interfaceA idea to make your user-defined exception
© - BASE, Inc.独⾃基底例外インターフェースを実装した個別の例外クラスを作成していくCreate exception classes that implement own base exception interfaceA idea to make your user-defined exception
© - BASE, Inc.• Throwableを継承した独⾃インターフェースをもって、⽣まれうる例外クラス全体の⼤きなスコープを作れる• You can create a large scope for the entire user-defined exceptionclassBenefits by making your user-defined exception
© - BASE, Inc.• 独⾃例外をスコープを意識して使うように促せる• Encourage developers to use user-defined exceptions with properscope in mind• 例:実⾏時or例外時、アプリケーションレイヤー …etc)• ex. Runtime or Inspection, application layer etcBenefits by making your user-defined exception
© - BASE, Inc.Summary• 適切なスコープで例外を使い分ける• Use different exceptions with appropriate scope• 正当性と堅牢性• Legitimacy and Robustness• それらを可能にするためのPHPでの基底例外• Base exception to make them possible in PHP
© - BASE, Inc.