一个static的问题

static可以放在class,也可以放在function。两者有什么差异。

一般当函数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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值