Hi there.
I'm trying to build site using OOP in PHP. Everyone is talking about Singleton, hermetization, MVC, and using exceptions. So I've tried to do it like this:
Class building whole site:
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 managing themes
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';
}
}
}
And usage:
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();
}
I don't like those exception handlers - i don't want to catch them like some pokemons... I want to use things simple: $core->theme->display('footer'); And if something is wrong, and debug mode is enabled, then aplication show error. What should i do?