挺難懂的解決方案

來源:互聯網
上載者:User
挺難懂的
下面這段代碼如何理解呢,最好在關節點上給與解釋
 * 行為模型執行個體
*
* @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();
}
}
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.