Skip to content

Instantly share code, notes, and snippets.

@bastman
Created November 20, 2012 14:34
Show Gist options
  • Save bastman/4118276 to your computer and use it in GitHub Desktop.
Save bastman/4118276 to your computer and use it in GitHub Desktop.
PHP Bootstrap Errorhandling Example
<?php
/**
* Created by JetBrains PhpStorm.
* User: seb
* Date: 11/20/12
* Time: 2:09 PM
* To change this template use File | Settings | File Templates.
*/
namespace BootstrapExample;
class Bootstrap
{
/**
* @var Bootstrap
*/
private static $_instance;
/**
* @return Bootstrap
*/
public static function getInstance()
{
if ((!self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* @return Bootstrap
*/
public function init()
{
ini_set("display_errors", true);
ini_set("html_errors", false);
error_reporting( (E_ALL | E_STRICT) ^ E_NOTICE); // ignore E_NOTICE
$errorCaptureLevel = error_reporting();
//$errorCaptureLevel = (E_ALL | E_STRICT);
set_error_handler(
function ($errno,
$errstr,
$errfile = null,
$errline = null,
$errcontext = null) {
throw new \ErrorException(
$errstr, $errno, 0, $errfile, $errline
);
},
$errorCaptureLevel
);
set_exception_handler(
array($this, 'handleException')
);
register_shutdown_function(
array($this, 'handleShutdown')
);
ini_set("display_errors", false);
return $this;
}
/**
* @param \Exception $exception
*/
public function handleException(\Exception $exception)
{
echo PHP_EOL . __METHOD__ . PHP_EOL;
// handle ErrorExceptions: error turned into exceptions
if($exception instanceof \ErrorException) {
switch($exception->getCode()) {
case E_NOTICE:
case E_WARNING:
case 0: {
// ignore
return;
break;
}
default: break;
}
}
// handle any other uncaught exception
// e.g. log it ?
}
/**
*
*/
public function handleShutdown()
{
echo PHP_EOL . __METHOD__ . PHP_EOL;
if(!error_get_last()) {
return;
}
//var_dump(error_get_last());
}
}
<?php
/**
* Created by JetBrains PhpStorm.
* User: seb
* Date: 11/20/12
* Time: 2:32 PM
* To change this template use File | Settings | File Templates.
*/
require dirname(__FILE__).'/BootstrapExample.php';
use BootstrapExample\Bootstrap;
$bootstrap = Bootstrap::getInstance();
$bootstrap->init();
echo '<pre>';
try {
var_dump(__FILE__ . ' : ' . __LINE__);
@include 'foo.php';
var_dump(__FILE__ . ' : ' . __LINE__);
$arr = array();
var_dump(@$arr[0]);
var_dump(__FILE__ . ' : ' . __LINE__);
}catch (\Exception $e) {
var_dump(__FILE__ . ' : ' . __LINE__);
var_dump('EXCEPTION CATCHED IN '.__FILE__.': '
. ' ' . get_class($e)
. ' ' . $e->getMessage()
);
//throw $e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment