Andrzej 'nAndy' Łukaszewski, Wikia Inc.
AKAI, 8 maja, 2012
As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. (...) a good part of my life was going to be spent finding errors in my own programs.Sir Maurice Wilkes
Błędy w naszych programach są czymś nie pożądanym ze względu na:
PHP Parse error: syntax error, unexpected '=' in test.php on line 2
PHP Notice: Undefined offset: 0 in test.php on line 4
PHP Warning: Invalid argument supplied for foreach() in test.php
on line 3
PHP Fatal error: Call to undefined function nothing() in test.php on line 3
PHP Fatal error: Uncaught exception 'Exception'
with message 'an exception' in test.php:3
<?php function getMyArrayData() { $data = $memc->get('my-data-in-memc'); if( is_null($data) ) { $data = $mysql->selectMyDataFromDB(); $memc->set('my-data-in-memc', $data); } return $data; } ?>
<?php function getMyArrayData() { $data = $memc->get('my-data-in-memc'); if( empty($data) ) { $data = $mysql->selectMyDataFromDB(); $memc->set('my-data-in-memc', $data); } return $data; } ?>
<?php function getMyArrayData() { $data = $memc->get('my-data-in-memc'); if( empty($data) ) { //what if $mysql->selectMyDataFromDB() returns array()? $data = $mysql->selectMyDataFromDB(); $memc->set('my-data-in-memc', $data); } return $data; } ?>
error_reporting
domyślnie: E_ALL & ~E_NOTICE
E_STRICT
display_errors
domyślnie: "1"
log_errors
domyślnie: "0"
error_log
domyślnie: NULL
int error_reporting ([ int $level ] )
mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )
callable set_exception_handler ( callable $exception_handler )
array debug_backtrace ( [ int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT [, int $limit = 0 ]] )
error_reporting = -1 display_errors = "0" log_errors = "1" error_log = '/var/www/log/php'
Bootstrap.php
public function __construct($application) { parent::__construct($application); MyApp_Error_Handler::set(); }
library/MyApp/Error/Handler.php
class MyApp_Error_Handler { public static function handle($errno, $errstr, $errfile, $errline) { if (!error_reporting()) return; throw new Exception($errstr . " in $errfile:$errline". $errno); } public static function set() { set_error_handler(array(__CLASS__, 'handle')); } }
$_ENV['SLIM_MODE'] = 'production';
$app = new Slim(array( 'mode' => 'production' )); $app->configureMode('production', function () use ($app) { $app->config(array( 'log.enable' => true, 'log.path' => '../logs', 'debug' => false )); }); $app->configureMode('development', function () use ($app) { $app->config(array( 'log.enable' => false, 'debug' => true )); });
app/config/config_dev.yml
, app/config/config_prod.yml
application/configs/application.ini
[production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Application" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1
Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that the good programmer uses tests to detect his mistakes as soon as possible.phpunit.de
array array_slice ( array $array, int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
Debugging Production Systems by Bryan Cantrill:
http://www.infoq.com/presentations/Debugging-Production-Systems
Podręcznik PHP:
http://www.php.net/manual/pl/
Podręcznik PHPUnit:
http://www.phpunit.de/manual/current/en/index.html
Prezentacja:
http://slides.wikia.net/akai/20120508/phpFatalError/