我试图在PHP中使用OOP构建网站。每个人都在谈论辛格尔顿,诠释,MVC和使用例外。所以,我一直试图做这样的:异常混淆
类建筑的整个网站:
class Core
{
public $is_core;
public $theme;
private $db;
public $language;
private $info;
static private $instance;
public function __construct($lang = 'eng', $theme = 'default')
{
if(!self::$instance)
{
try
{
$this->db = new sdb(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
}
catch(PDOException $e)
{
throw new CoreException($e->getMessage());
}
try
{
$this->language = new Language($lang);
}
catch(LangException $e)
{
throw new CoreException($e->getMessage());
}
try
{
$this->theme = new Theme($theme);
}
catch(ThemeException $e)
{
throw new CoreException($e->getMessage());
}
}
return self::$instance;
}
public function getSite($what)
{
return $this->language->getLang();
}
private function __clone() { }
}
管理类主题
class Theme
{
private $theme;
public function __construct($name = 'default')
{
if(!is_dir("themes/$name"))
{
throw new ThemeException("Unable to load theme $name");
}
else
{
$this->theme = $name;
}
}
public function getTheme()
{
return $this->theme;
}
public function display($part)
{
if(!is_file("themes/$this->theme/$part.php"))
{
throw new ThemeException("Unable to load theme part: themes/$this->theme/$part.php");
}
else
{
return 'So far so good';
}
}
}
与用法:
error_reporting(E_ALL);
require_once('config.php');
require_once('functions.php');
try
{
$core = new Core();
}
catch(CoreException $e)
{
echo 'Core Exception: '.$e->getMessage();
}
echo $core->theme->getTheme();
echo "<br />";
echo $core->language->getLang();
try
{
$core->theme->display('footer');
}
catch(ThemeException $e)
{
echo $e->getMessage();
}
我不像那些异常处理程序 - 我不想像一些小宠物一样抓住它们...我想用简单的东西: $ core-> theme-> display('footer'); 如果有问题,并且调试模式已启用,则应用程序显示错误。我该怎么办?
嗯,我刚刚开始,但这种多重捕捉缩短了我的代码!我已经使用了CoreException,因为我不知道这个多重捕获 – Misiur 2010-05-22 14:57:33