Home >Backend Development >PHP Tutorial >挺难懂的解决方法

挺难懂的解决方法

WBOY
WBOYOriginal
2016-06-13 12:20:46726browse

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

 * 行为模型实例<br /> *<br /> * @param string $model 模型名称<br /> * @return obj 对象形式的返回结果<br /> */<br />function Logic($model = null, $base_path = null){<br />    static $_cache = array();<br />    $cache_key = $model.'.'.$base_path;<br />    if (!is_null($model) && isset($_cache[$cache_key])) return $_cache[$cache_key];<br />    $base_path = $base_path == null ? BASE_DATA_PATH : $base_path;<br />    $file_name = $base_path.'/logic/'.$model.'.logic.php';<br />    $class_name = $model.'Logic';<br />    if (!file_exists($file_name)){<br />        return $_cache[$cache_key] =  new Model($model);<br />    }else{<br />        require_once($file_name);<br />        if (!class_exists($class_name)){<br />            $error = 'Logic Error:  Class '.$class_name.' is not exists!';<br />            throw_exception($error);<br />        }else{<br />            return $_cache[$cache_key] = new $class_name();<br />        }<br />    }<br />}

------解决思路----------------------
<br />function Logic($model = null, $base_path = null){<br />	//定义静态变量<br />    static $_cache = array();<br />	//定义缓存key值<br />    $cache_key = $model.'.'.$base_path;<br />	//若是静态变量中有这个模型的实例就直接返回<br />    if (!is_null($model) && isset($_cache[$cache_key])) return $_cache[$cache_key];<br />	//组织类文件路径<br />    $base_path = $base_path == null ? BASE_DATA_PATH : $base_path;<br />    $file_name = $base_path.'/logic/'.$model.'.logic.php';<br />    $class_name = $model.'Logic';<br />	//类文件是否存在<br />    if (!file_exists($file_name)){<br />		//不存在就实例一个model<br />        return $_cache[$cache_key] =  new Model($model);<br />    }else{<br />		//存在就引入<br />        require_once($file_name);<br />		//判断是否存在 该类<br />        if (!class_exists($class_name)){<br />			//不存在就抛出异常<br />            $error = 'Logic Error:  Class '.$class_name.' is not exists!';<br />            throw_exception($error);<br />        }else{<br />			//存在就实例化它,存入静态数组中并返回<br />            return $_cache[$cache_key] = new $class_name();<br />        }<br />    }<br />}<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn