Think::instance PHP Method

instance() public static method

取得对象实例 支持调用类的静态方法
public static instance ( string $class, string $method = '' ) : object
$class string 对象类名
$method string 类的静态方法名
return object
    public static function instance($class, $method = '')
    {
        $identify = $class . $method;
        if (!isset(self::$_instance[$identify])) {
            if (class_exists($class)) {
                $o = new $class();
                if (!empty($method) && method_exists($o, $method)) {
                    self::$_instance[$identify] = call_user_func_array(array(&$o, $method));
                } else {
                    self::$_instance[$identify] = $o;
                }
            } else {
                halt(L('_CLASS_NOT_EXIST_') . ' = ' . $class . ' = ' . $method);
            }
        }
        return self::$_instance[$identify];
    }

Usage Example

 /**
 +----------------------------------------------------------
 * 渲染模板输出
 +----------------------------------------------------------
 * @access public
 +----------------------------------------------------------
 * @param string $templateFile 模板文件名
 * @param array $var 模板变量
 * @param string $charset 模板输出字符集
 * @param string $varPrefix 模板变量前缀
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
 public function fetch($templateFile, $var, $charset)
 {
     // 缓存无效 重新编译
     $tpl = Think::instance('ThinkTemplate');
     // 编译并加载模板文件
     $tpl->load($templateFile, $var, $charset);
 }
All Usage Examples Of Think::instance