【ThinkPHP】实例化模型的方法
歡迎大家訪問(wèn)我自己架的博客站點(diǎn)?碼廄技術(shù)博客!
1、實(shí)例化一個(gè)Model
function D($name='', $app='') {
static $_model = array();
if (empty($name))
return new Model;
if (empty($app))
$app = C('DEFAULT_APP');
if (isset($_model[$app . $name]))
return $_model[$app . $name];
$OriClassName = $name;
if (strpos($name, '.')) { ? ? ?? // 確認(rèn)Model類文件名是不是aaa.bbb.ccc結(jié)構(gòu)
$array = explode('.', $name);
$name = array_pop($array); // 對(duì)于類似aaa.bbb.ccc結(jié)構(gòu)文件名的Model類,只取到第一節(jié)
$className = $name . 'Model';
import($app . '.Model.' . implode('.', $array) . '.' . $className);
} else {
$className = $name . 'Model';
import($app . '.Model.' . $className);
}
if (class_exists($className)) {
$model = new $className();
} else {
$model = new Model($name);
}
$_model[$app . $OriClassName] = $model;
return $model;
}
2、實(shí)例化一個(gè)沒(méi)有模型文件的Model
function M($name='', $class='Model') {
static $_model = array();
if (!isset($_model[$name . '_' . $class]))
$_model[$name . '_' . $class] = new $class($name);
return $_model[$name . '_' . $class];
}
3、Model類構(gòu)造函數(shù)
public function __construct($name='',$connection='') {
// 模型初始化
$this->_initialize();
// 獲取模型名稱
if(!empty($name)) {
$this->name = $name;
}elseif(empty($this->name)){
$this->name = $this->getModelName();
}
// 數(shù)據(jù)庫(kù)初始化操作
// 獲取數(shù)據(jù)庫(kù)操作對(duì)象
// 當(dāng)前模型有獨(dú)立的數(shù)據(jù)庫(kù)連接信息
$this->db(0,empty($this->connection)?$connection:$this->connection);
// 設(shè)置表前綴
$this->tablePrefix = $this->tablePrefix?$this->tablePrefix:C('DB_PREFIX');
$this->tableSuffix = $this->tableSuffix?$this->tableSuffix:C('DB_SUFFIX');
// 字段檢測(cè)
if(!empty($this->name) && $this->autoCheckFields) $this->_checkTableInfo();
}
總結(jié):
轉(zhuǎn)載于:https://www.cnblogs.com/catroll/archive/2011/11/23/model_of_thinkphp.html
總結(jié)
以上是生活随笔為你收集整理的【ThinkPHP】实例化模型的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 超星章节内ppt课件下载
- 下一篇: jsoup测试例子