SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
The talk is designed to give an entry-level introduction to how you should be handling errors, exceptions and how to effectively log in an application.
The talk is designed to give an entry-level introduction to how you should be handling errors, exceptions and how to effectively log in an application.
3.
● Something broke… :)
● e.g.
○ Can’t connect to MySQL (mysqli_connect)
○ No such file or directory (fopen)
● Usually from PHP core
● Sometimes fatal (stop execution)
What are errors?
4.
Types of PHP Errors
● E_ERROR
● E_WARNING
● E_NOTICE
● E_PARSE
● Others (E_STRICT, E_DEPRECATED) etc.
● User errors (E_USER_ERROR etc.)
5.
E_ERROR
<?php
foo();
// Fatal error: Call to undefined
function foo() in /in//in/N2gbL on
line 3
6.
E_WARNING
<?php
fopen('foo', 'r');
// Warning: fopen(foo): failed to
open stream: No such file or
directory in /in//in/tZHGY on line
3
7.
E_NOTICE
<?php
$a = $b;
// Notice: Undefined variable: b in
/in//in/9dPC5 on line 3
8.
E_PARSE
<?php
x c
// Parse error: syntax error,
unexpected 'c' (T_STRING) in
/in//in/fkEaj on line 3
10.
Problems.
● Depends on “error_reporting” php.ini setting
● Displaying errors is UGLY
● Existence of “@” operator
● Only logs to file or syslog
● Easily ignored
● Not very “OO”
11.
Ways Around
// Will raise E_NOTICE
fopen($somefile, 'r');
// No error! :)
if (file_exists($somefile)) {
fopen($somefile, 'r');
} else {
// nice handling...
}
13.
● Something still broke
● Wider scope:
○ Logic errors
○ Flow control (for errors)
● “Catchable”
● Turn into fatal errors if not caught
● They are classes (can make your own)
● Common in other OO languages
What are exceptions?
14.
Jargon Buster
● throw
Triggering an exception
● try
Try to run a piece of code which *may* throw an
exception
● catch
Handle an exception
● finally
Always run some code after a try/catch block
15.
● Built in to PHP
● More descriptive than just “Exception”, e.g.:
○ InvalidArgumentException
○ LogicException
○ OutOfBoundsException
○ RuntimeException
○ see PHP manual for more
SPL Exceptions
16.
Example (throw)
class Division
{
public function divide($a, $b)
{
if ($b == 0) {
throw new Exception(‘div by zero’);
}
return ($a / $b);
}
}
28.
Capturing Logging
Use these and send output to $logger
● set_exception_handler()
○ Handles all uncaught exceptions
● set_error_handler()
○ Handles most errors
● register_shutdown_function()
○ Handles fatal errors
29.
Sending Log Messages
● PSR-3 makes it easy
● However you want…
● Monolog has loads:
○ syslog-compatible / error_log
○ Email, HipChat
○ AMQP, Sentry, Zend Monitor, Graylog2
○ Redis, MongoDB, CouchDB
30.
Summary
● PHP generates errors usually
● Exceptions are great in OOP context
● More control with exceptions
● Logging is important
● Logging is easy
● Short term investment, long term benefit
● NO EXCUSES!