2008年8月27日 星期三

Using a Singleton with Smarty (syntax and usage questions)



$smarty = MySmarty::getInstance();
$smarty->testing(); #does work


class MySmarty
{

public static function getInstance()
{
if (self :: $instance === NULL)
{
self :: $instance = new MySmarty();
include('Smarty.class.php' );

self :: $smarty = New Smarty;
self :: $smarty -> caching = false;
self :: $smarty -> template_dir = $SMARTY_TEMPLATE_PATH."/templates/";
self :: $smarty -> compile_dir = $SMARTY_TEMPLATE_PATH."/templates_c/";
self :: $smarty -> config_dir = $SMARTY_TEMPLATE_PATH."/configs/";
self :: $smarty -> cache_dir = $SMARTY_TEMPLATE_PATH."/cache/";
self :: $smarty -> assign('app_name', 'HTML Email');
self :: $smarty -> compile_check = true;
self :: $smarty -> debugging = false;

}
return self :: $instance;
}

private function __clone()//do not allow clone
{
}

static private $smarty = false;
static private $instance = NULL;



// Example method
public function testing()
{
echo 'Testing!';
}
}


摘自:http://www.phpinsider.com/smarty-forum/viewtopic.php?t=13781&highlight=static

沒有留言:

wibiya widget