Hi, I'm writing a CMS and I'm having trouble making PHP handle errors the way I want it to. The code looks fine to me, but PHP acts like it doesn't see it.
I'm including just the necessary parts of the files in question; see if you can tell me what I'm doing wrong.
index.php
include 'core/Error.class.php';
require 'core/DB.class.php';
$db = DB::singleton();
core/DB.class.php
class DB
{
protected $mydb;
private static $instance;
private function __construct () {
$this->mydb = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_NAME,MYSQL_PORT)
or Error::mysqli_connect();
}
}
static function singleton () {
if (!isset(self::$instance)) {
self::$instance = new self();
} return self::$instance;
}
}
core/Error.class.php
define('E_FATAL', E_USER_ERROR);
define('E_NONFATAL', E_USER_WARNING);
define('E_SUGGESTION', E_USER_NOTICE);
class Error {
static $codes = array(400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error');
static function send ($httpcode,$severity,$text) {
if (!headers_sent()) header("HTTP/1.0 $httpcode {$codes[$httpcode]}");
trigger_error($text,$severity);
}
static function mysqli_connect () {
self::send(500,E_FATAL,'Could not connect to database.<br>Error '.mysqli_connect_errno().': '.mysqli_connect_error());
}
}
Generated output:
Expected output: