【发布时间】:2011-01-02 15:11:49
【问题描述】:
嘿,我需要建议。我写的这个小函数是“好”还是会占用资源? 它的使用方式如下:
$login = load_class('LoginClass');
或
load_class('LoginClassTwo', false); // for singletons and stuff
$loginTwo = LoginClassTwo::getInstance();
这是函数
function load_class($class, $instantiate = TRUE){
static $obj = array(); // holds the instancec of the classes
$l = strtolower($class); // the name of the file is the name of the class but lowercased
if (isSet($obj[$l])) { // Do we have an instance?
return $obj[$l];
}
$file = 'classess/' . $l . '.class.php';
if (file_exists($file) && is_readable($file)) { // Can we read the file?
include $file;
if ($instantiate == FALSE) { // Do we need to instantiate?
$obj[$l] = TRUE;
} else {
$obj[$l] = new $class;
}
return $obj[$l];
}
return FALSE; }
我担心这种方法无效并且会消耗太多内存还是我错了?有没有更好的做法?
【问题讨论】:
标签: php design-patterns memory-leaks classloader