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

Exception Handling in PHP

52034cfb48ca73e13e130f2711ec41f0?s=47 Frank Neff
November 06, 2014

Exception Handling in PHP

A lightning Talk about exception handling best practices in PHP for one of our customers

52034cfb48ca73e13e130f2711ec41f0?s=128

Frank Neff

November 06, 2014
Tweet

Transcript

  1. try { Exception Handling in PHP } A brief introduction

  2. Exception != Error

  3. – docs.oracle.com “An exception is an event, which occurs during

    the execution of a program, that disrupts the normal flow of the program's instructions.”
  4. – php.net/manual “When an exception is thrown, code following the

    statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message”
  5. Application Layers Presentation Business Data NoDatabaseConnectionException NoResultException EmptyResultException InvalidConfigurationException HTTP/404

    HTTP/500
  6. Example EVIL!

  7. – Me “Because a DBAL does not know how to

    handle an empty result… But the business logic does!”
  8. Done right

  9. Done right

  10. Pass Exceptions through layers

  11. Exceptions

  12. DBAL

  13. Repository

  14. Controller

  15. – OSS Development Wisdom “An API method should always return

    an expected result or throw an exception.”
  16. Thanks ;) frankneff.ch - @frank_neff - github.com/frne