static可以放在class,也可以放在function。两者有什么差异。
一般当函数do1,do2都用到 $_arr时,把它作为类变量。
Codeigniter 用在函数load_class, 变量static $_classes = array();
于是在code时发现个问题
设计想法是用借用$_files把Module和db都管理起来,结果发现不行。
仔细查看后将static $_files 变成类变量就解决问题了。
有兴趣的可以自己写code玩玩看。
框架文件多了,类实例后也很多。所以唯一就显得比较重要。
当然你也可以学习zend framework 的 require_once, set_include_path
现在的filemanage.php
一般当函数do1,do2都用到 $_arr时,把它作为类变量。
class A{
static $_arr;
function do1(){
}
function do2(){
}
}
Codeigniter 用在函数load_class, 变量static $_classes = array();
于是在code时发现个问题
class FileManage{
function install(){
static $_files;
}
}
class ModuleManage extends FileManage{}
class DbManage extends FileManage{}
设计想法是用借用$_files把Module和db都管理起来,结果发现不行。
仔细查看后将static $_files 变成类变量就解决问题了。
有兴趣的可以自己写code玩玩看。
框架文件多了,类实例后也很多。所以唯一就显得比较重要。
当然你也可以学习zend framework 的 require_once, set_include_path
现在的filemanage.php
// 文件管理
class CFileManage{
static $_files;
// 常规install filepath = path.file.".php"
function install($filetype, $file, $path=''){
if(is_array($file)) {
foreach($file as $f){
$this->_installFile($filetype, $f, $path);
}
}elseif(!is_string($file)){
return;
}else{
$this->_installFile($filetype, $file, $path);
}
}
function _installFile($filetype, $file, $path=""){
if(isset(self::$_files[$filetype][$file])){
// echo "\r\ninstalled $file\r\n";
return self::$_files[$filetype][$file];
}
$filepath = $path . $file . ".php";
// echo $filepath."\r\n";
if(!file_exists($filepath)){
// file not exist
throw new Exception("not find file:{$filepath}");
}
global $g_loaded;
$g_loaded[$filetype][$file] = $filepath;
self::$_files[$filetype][$file] = include $filepath;
}
}