I have the following function to load my libraries:
function load_class($name) {
$namePath = explode('_', $name);
$filePath = '';
if (is_array($namePath)) {
for ($i=0; $i<sizeof($namePath); $i++) {
$filePath .= $namePath[$i];
if ($i != sizeof($namePath) - 1) $filePath .= '/';
}
} else $filePath = $name;
if (is_file($filePath . '.php')) require_once($filePath . '.php');
else if (is_file($filePath . '.class.php')) require_once($filePath . '.class.php');
else throw new Exception('Unable to load class: ' . $name . '. No such file or a directory. ');
if (!class_exists($name)) throw new Exception('Class: ' . $name . ' doesn\'t exists. ');
}
And autoload:
function __construct() {
try {
spl_autoload_register(array($this, 'load_class'));
} catch (Exception $e) {
echo $e -> getMessage();
}
}
But unfortunatelly exceptions doesn't prevent me from getting fatal errors like there is no class or there is no file. Is there an elegant way to just show user my own communicate, not the php error?