/** * 输出页面 * @param tplname 模板文件路径 */ public function display($tplname) { try { $this->addfuncs(); $this->displayed = TRUE; if ($GLOBALS['G']['view']['debugging'] && g_DEBUG) { $this->engine->debugging = TRUE; } $this->engine->display($tplname); } catch (Exception $e) { gError($GLOBALS['G']['view']['engine_name'] . ' Error: ' . $e->getMessage()); } }
/** * 魔术函数,实现对控制器扩展类的自动加载 */ public function __call($name, $args) { if (in_array($name, $GLOBALS['G']["auto_load_controller"])) { return gClass($name)->__input($args); } elseif (!method_exists($this, $name)) { gError("方法 {$name}未定义!<br />请检查是否控制器类(" . get_class($this) . ")与数据模型类重名?"); } }
/** * 替换数据,根据条件替换存在的记录,如记录不存在,则将条件与替换数据相加并新增一条记录。 * * @param conditions 数组形式,查找条件,请注意,仅能使用数组作为该条件! * @param row 数组形式,修改的数据 */ public function replace($conditions, $row) { if ($this->find($conditions)) { return $this->update($conditions, $row); } else { if (!is_array($conditions)) { gError('replace方法的条件务必是数组形式!'); } $rows = gConfigReady($conditions, $row); return $this->create($rows); } }
/** gClass 类实例化函数 自动载入类定义文件,实例化并返回对象句柄 * * @param class_name 类名称 * @param args 类初始化时使用的参数,数组形式 * @param sdir 载入类定义文件的路径,可以是目录+文件名的方式,也可以单独是目录。sdir的值将传入import()进行载入 * @param force_inst 是否强制重新实例化对象 */ function gClass($class_name, $args = null, $sdir = null, $force_inst = FALSE) { // 检查类名称是否正确,以保证类定义文件载入的安全性 if (preg_match('/[^a-z0-9\\-_.]/i', $class_name)) { gError($class_name . "类名称错误,请检查。"); } // 检查是否该类已经实例化,直接返回已实例对象,避免再次实例化 if (TRUE != $force_inst) { if (isset($GLOBALS['G']["inst_class"][$class_name])) { return $GLOBALS['G']["inst_class"][$class_name]; } } // 如果$sdir不能读取,则测试是否仅路径 if (null != $sdir && !gImport($sdir) && !gImport($sdir . "/{$class_name}.php")) { return FALSE; } $has_define = FALSE; // 检查类定义是否存在 if (class_exists($class_name, false) || interface_exists($class_name, false)) { $has_define = TRUE; } else { if (TRUE == gImport($class_name . '.php')) { $has_define = TRUE; } } if (FALSE != $has_define) { $argString = ''; $comma = ''; if (null != $args) { for ($i = 0; $i < count($args); $i++) { $argString .= $comma . "\$args[{$i}]"; $comma = ', '; } } eval("\$GLOBALS['G']['inst_class'][\$class_name]= new \$class_name({$argString});"); return $GLOBALS['G']["inst_class"][$class_name]; } gError($class_name . "类定义不存在,请检查。"); }
/** * 连接数据库方法 * @param type $dbConfig */ public function connect($db) { $this->dbCurrent = $db; $linkfunction = TRUE == $db['persistent'] ? 'mysql_pconnect' : 'mysql_connect'; $this->conn = $linkfunction($db['host'], $db['login'], $db['password']); if (!$this->conn) { gError("数据库链接错误 : " . mysql_error()); } mysql_select_db($db['database'], $this->conn) || gError("无法使用数据库 : " . mysql_error()); mysql_query("SET NAMES '" . $db['charset'] . "'", $this->conn); }