次の代替記述の目的は何ですか?
次の記述の問題 1: コンストラクターは実際には空ですが、さらに珍しいのは、コンストラクターが空の場合、それをインスタンス化する方法です。以下
その理由は何ですか?
>この書き方はシングルトンモードです。
<?php<br />/**<br /> * 模板驱动<br /> *<br /> * 模板驱动,商城模板引擎<br /> *<br /> *<br /> * @package tpl<br /> * @copyright Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)<br /> * @license http://www.shopnc.net<br /> * @link http://www.shopnc.net<br /> * @author ShopNC Team<br /> * @since File available since Release v1.1<br /> */<br />defined('InShopNC') or exit('Access Invalid!');<br />class Tpl{<br /> /**<br /> * 单件对象<br /> */<br /> private static $instance = null;<br /> /**<br /> * 输出模板内容的数组,其他的变量不允许从程序中直接输出到模板<br /> */<br /> private static $output_value = array();<br /> /**<br /> * 模板路径设置<br /> */<br /> private static $tpl_dir='';<br /> /**<br /> * 默认layout<br /> */<br /> private static $layout_file = 'layout';<br /> <br /> private function __construct(){}<br /> <br /> /**<br /> * 实例化<br /> *<br /> * @return obj<br /> */<br /> public static function getInstance(){<br /> if (self::$instance === null || !(self::$instance instanceof Tpl)){<br /> self::$instance = new Tpl();<br /> }<br /> return self::$instance;<br /> }<br /> <br /> /**<br /> * 设置模板目录<br /> *<br /> * @param string $dir<br /> * @return bool<br /> */<br /> public static function setDir($dir){<br /> self::$tpl_dir = $dir;<br /> return true;<br /> }<br /> /**<br /> * 设置布局<br /> *<br /> * @param string $layout<br /> * @return bool<br /> */<br /> public static function setLayout($layout){<br /> self::$layout_file = $layout;<br /> return true;<br /> }<br /> <br /> /**<br /> * 抛出变量<br /> *<br /> * @param mixed $output<br /> * @param void<br /> */<br /> public static function output($output,$input=''){<br /> self::getInstance();<br /> <br /> self::$output_value[$output] = $input;<br /> }<br /> <br /> /**<br /> * 调用显示模板<br /> *<br /> * @param string $page_name<br /> * @param string $layout<br /> * @param int $time<br /> */<br /> public static function showpage($page_name='',$layout='',$time=2000){<br /> if (!defined('TPL_NAME')) define('TPL_NAME','default');<br /> self::getInstance();<br /> if (!empty(self::$tpl_dir)){<br /> $tpl_dir = self::$tpl_dir.DS;<br /> }<br /> //默认是带有布局文件<br /> if (empty($layout)){<br /> $layout = 'layout'.DS.self::$layout_file.'.php';<br /> }else {<br /> $layout = 'layout'.DS.$layout.'.php';<br /> }<br /> $layout_file = BASE_PATH.'/templates/'.TPL_NAME.DS.$layout;<br /> $tpl_file = BASE_PATH.'/templates/'.TPL_NAME.DS.$tpl_dir.$page_name.'.php';<br /> if (file_exists($tpl_file)){<br /> //对模板变量进行赋值<br /> $output = self::$output_value;<br /> //页头<br /> $output['html_title'] = $output['html_title']!='' ? $output['html_title'] :$GLOBALS['setting_config']['site_name'];<br /> $output['seo_keywords'] = $output['seo_keywords']!='' ? $output['seo_keywords'] :$GLOBALS['setting_config']['site_name'];<br /> $output['seo_description'] = $output['seo_description']!='' ? $output['seo_description'] :$GLOBALS['setting_config']['site_name'];<br /> $output['ref_url'] = getReferer();<br /><br /> Language::read('common');<br /> $lang = Language::getLangContent();<br /><br /> @header("Content-type: text/html; charset=".CHARSET);<br /> //判断是否使用布局方式输出模板,如果是,那么包含布局文件,并且在布局文件中包含模板文件<br /> if ($layout != ''){<br /> if (file_exists($layout_file)){<br /> include_once($layout_file);<br /> }else {<br /> $error = 'Tpl ERROR:'.'templates'.DS.$layout.' is not exists';<br /> throw_exception($error);<br /> }<br /> }else {<br /> include_once($tpl_file);<br /> }<br /> }else {<br /> $error = 'Tpl ERROR:'.'templates'.DS.$tpl_dir.$page_name.'.php'.' is not exists';<br /> throw_exception($error);<br /> }<br /> }<br /> /**<br /> * 显示页面Trace信息<br /> *<br /> * @return array<br /> */<br /> public static function showTrace(){<br /> $trace = array();<br /> //当前页面<br /> $trace[Language::get('nc_debug_current_page')] = $_SERVER['REQUEST_URI'].'<br>';<br /> //请求时间<br /> $trace[Language::get('nc_debug_request_time')] = date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).'<br>';<br /> //系统运行时间<br /> $query_time = number_format((microtime(true)-StartTime),3).'s';<br /> $trace[Language::get('nc_debug_execution_time')] = $query_time.'<br>';<br /> //内存<br /> $trace[Language::get('nc_debug_memory_consumption')] = number_format(memory_get_usage()/1024/1024,2).'MB'.'<br>';<br /> //请求方法<br /> $trace[Language::get('nc_debug_request_method')] = $_SERVER['REQUEST_METHOD'].'<br>';<br /> //通信协议<br /> $trace[Language::get('nc_debug_communication_protocol')] = $_SERVER['SERVER_PROTOCOL'].'<br>';<br /> //用户代理<br /> $trace[Language::get('nc_debug_user_agent')] = $_SERVER['HTTP_USER_AGENT'].'<br>';<br /> //会话ID<br /> $trace[Language::get('nc_debug_session_id')] = session_id().'<br>';<br /> //执行日志<br /> $log = Log::read();<br /> $trace[Language::get('nc_debug_logging')] = count($log)?count($log).Language::get('nc_debug_logging_1').'<br/>'.implode('<br/>',$log):Language::get('nc_debug_logging_2');<br /> $trace[Language::get('nc_debug_logging')] = $trace[Language::get('nc_debug_logging')].'<br>';<br /> //文件加载<br /> $files = get_included_files();<br /> $trace[Language::get('nc_debug_load_files')] = count($files).str_replace("\n",'<br/>',substr(substr(print_r($files,true),7),0,-2)).'<br>';<br /> return $trace;<br /> }<br />}<br />