Mailinglisten-Archive |
Hi Sebastian,
$singleton muss nicht mal auf NULL gesetzt werden, bedingt aber bei
Rückgabe eine Prüfng ob in $singleton was drin steht oder nciht. Hier
mal mein bissl Singleton-Code. Funktioniert bei mir wunderbar:
<Code>
function &singleton($class) {
static $singleton;
if (empty($singleton[$class]) || !is_object($singleton[$class]))
$singleton[$class] = new singleton($class);
return $singleton[$class]->instances[$class];
}
class singleton {
var $pointer = '';
var $instances = array();
function singleton($class) {
if (!$this->getInstance($class)) {
$this->setInstance($class);
}
return !empty($this->instances[$class]) ?
$this->instances[$class] : NULL;
}
function setInstance($class) {
$file = $class.'.class.inc.php';
$exists = common::checkFileExists($file, explode(';',
ini_get('include_path')));
if ($exists) {
require_once($file);
if (class_exists($class)) $this->instances[$class] = new
$class();
else common::ErrorOutput('Class "'.$class.'" doesn\'t
exists!', '', debug_backtrace());
} else common::ErrorOutput('Required file "'.$file.'" doesn\'t
exists!', '', debug_backtrace());
}
function getInstance($class) {
if (!empty($this->instances[$class])) {
if (is_object($this->instances[$class])) {
$this->$pointer =& $this->instances[$class];
return $this->pointer;
}
}
}
}
</Code>
Die Singleton- und Common-Klasse befinden sich bei mir in der selben
Datei. Ggf. kann man auch "die()" verwenden...
--
Mit freundlichen Grüßen / Kind regards
Steffen Kother
php::bar PHP Wiki - Listenarchive