细说php这么难懂,挺难懂的解决方法
挺難懂的
下面這段代碼如何理解呢,最好在關(guān)節(jié)點上給與解釋
*?行為模型實例
*
*?@param?string?$model?模型名稱
*?@return?obj?對象形式的返回結(jié)果
*/
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){
//定義靜態(tài)變量
static?$_cache?=?array();
//定義緩存key值
$cache_key?=?$model.'.'.$base_path;
//若是靜態(tài)變量中有這個模型的實例就直接返回
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{
//存在就實例化它,存入靜態(tài)數(shù)組中并返回
return?$_cache[$cache_key]?=?new?$class_name();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的细说php这么难懂,挺难懂的解决方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#的四舍五入函数
- 下一篇: 《Python爬虫开发与项目实战》——第