08-18-2008, 02:25 AM
|
#8 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
I personally use a mix of the Singleton and Registry pattern with some property overloading.
Example:
PHP Code:
<?php
final class Project
{
protected static $instance;
protected static $objects = Array();
private function __construct()
{
}
private function __clone()
{
}
public function __get($component)
{
$component = strtolower((string) $component);
if(array_key_exists($component, self::$objects))
{
return(self::$objects[$component]);
}
return(NULL);
}
public static function init()
{
if(!is_object(self::$instance))
{
self::$instance = new self;
}
return(self::$instance);
}
public static function load($component)
{
$component = strtolower((string) $component);
if(array_key_exists($component, self::$objects))
{
return(true);
}
$class = 'Project_Component_' . $component;
if(!class_exists($class))
{
throw new Project_Exception('Component class not found!');
}
self::$objects[$component] = new $class;
return(self::$objects[$component]);
}
}
class Project_Exception extends Exception
{
}
?>
This does not introduce component constructor arguments, but its simply a mix. I hope this could help you abit ;)
__________________
|
|
|