PHPCMS V9代码解析:
$this->cdb = pc_base::load_model('admin_model');
调用:
/**
* 加载数据模型
* @param string $classname 类名
*/
public static function load_model($classname) {
return self::_load_class($classname,'model');
}
这个时候传输参数‘model’,继续调用_load_class:
/**
* 加载类文件函数
* @param string $classname 类名
* @param string $path 扩展地址
* @param intger $initialize 是否初始化
*/
private static function _load_class($classname, $path = '', $initialize = 1) {
static $classes = array();
if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'classes';
$key = md5($path.$classname);
if (isset($classes[$key])) {
if (!empty($classes[$key])) {
return $classes[$key];
} else {
return true;
}
}
if (file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) {
include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php';
$name = $classname;
if ($my_path = self::my_path(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) {
include $my_path;
$name = 'MY_'.$classname;
}
if ($initialize) {
$classes[$key] = new $name;
} else {
$classes[$key] = true;
}
return $classes[$key];
} else {
return false;
}
}
通过include 引入model文件夹下的源代码类admin_model.
具体源代码如下:
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class admin_model extends model {
public function __construct() {
$this->db_config = pc_base::load_config('database');
$this->db_setting = 'default';
$this->table_name = 'admin';
parent::__construct();
}
}
?>
具体源代码如下:
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class admin_model extends model {
public function __construct() {
$this->db_config = pc_base::load_config('database');
$this->db_setting = 'default';
$this->table_name = 'admin';
parent::__construct();
}
}
?>
父类 mode的关键源代码为:
<?php
/**
* model.class.php 数据模型基类
*
* @copyright (C) 2005-2010 PHPCMS
* @license http://www.phpcms.cn/license/
* @lastmodify 2010-6-7
*/
pc_base::load_sys_class('db_factory', '', 0);
class model {
//数据库配置
protected $db_config = '';
//数据库连接
protected $db = '';
//调用数据库的配置项
protected $db_setting = 'default';
//数据表名
protected $table_name = '';
//表前缀
public $db_tablepre = '';
public function __construct() {
if (!isset($this->db_config[$this->db_setting])) {
$this->db_setting = 'default';
}
$this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name;
$this->db_tablepre = $this->db_config[$this->db_setting]['tablepre'];
$this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);
}
在类db_factory 中使用静态方法获取数据库连接实例,
final class db_factory {
/**
* 当前数据库工厂类静态实例
*/
private static $db_factory;
/** * 数据库配置列表 */
protected $db_config = array();
/** * 数据库操作实例化列表 */
protected $db_list = array();
/** * 构造函数 */
public function __construct()
{ }
没有评论:
发表评论