• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

挺难懂的

php 搞代码 3年前 (2022-01-23) 10次浏览 已收录 0个评论
文章目录[隐藏]

下面这段代码如何理解呢,最好在关节点上给与解释

 * 行为模型实例 * * @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($mode<div>……本2文来源gaodai.ma#com搞##代!^码@网3</div><code>搞代gaodaima码</code>l) && 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();        }    }}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:挺难懂的
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址