挺難懂的
下面這段代碼如何理解呢,最好在關節點上給與解釋
* 行為模型執行個體
*
* @param string $model 模型名稱
* @return obj 對象形式的返回結果
*/
function Logic($model = null, $base_path = null){
static $_cache = array();
$cache_key = $model.'.'.$base_path;
if (!is_null($model) && isset($_cache[$cache_key])) return $_cache[$cache_key];
$base_path = $base_path == null ? BASE_DATA_PATH : $base_path;
$file_name = $base_path.'/logic/'.$model.'.logic.php';
$class_name = $model.'Logic';
if (!file_exists($file_name)){
return $_cache[$cache_key] = new Model($model);
}else{
require_once($file_name);
if (!class_exists($class_name)){
$error = 'Logic Error: Class '.$class_name.' is not exists!';
throw_exception($error);
}else{
return $_cache[$cache_key] = new $class_name();
}
}
}
------解決思路----------------------
function Logic($model = null, $base_path = null){
//定義靜態變數
static $_cache = array();
//定義緩衝key值
$cache_key = $model.'.'.$base_path;
//若是靜態變數中有這個模型的執行個體就直接返回
if (!is_null($model) && isset($_cache[$cache_key])) return $_cache[$cache_key];
//組織類檔案路徑
$base_path = $base_path == null ? BASE_DATA_PATH : $base_path;
$file_name = $base_path.'/logic/'.$model.'.logic.php';
$class_name = $model.'Logic';
//類檔案是否存在
if (!file_exists($file_name)){
//不存在就執行個體一個model
return $_cache[$cache_key] = new Model($model);
}else{
//存在就引入
require_once($file_name);
//判斷是否存在 該類
if (!class_exists($class_name)){
//不存在就拋出異常
$error = 'Logic Error: Class '.$class_name.' is not exists!';
throw_exception($error);
}else{
//存在就執行個體化它,存入靜態數組中並返回
return $_cache[$cache_key] = new $class_name();
}
}
}