Give me Suggestions and opinions.
also a usage example below....
<?php
namespace Loader;
interface iRegistery
{
public static function Init();
public static function Load($classname);
}
final class Registry implements iRegistery
{
private static $array=array();
private static $instance;
private static $library;
public static $use;
private function __construct()
{
}
private function __clone()
{
}
final public static function Init()
{
if(!self::$instance instanceof self)
{
self::$instance = new self();
}
return self::$instance;
}
public static function RegisterClass($classname , $lib=NULL)
{
static $path;
if($lib !== NULL)
{
self::$instance->library = $lib;
}
elseif($lib === NULL or $lib === 'defualt')
{
self::$instance->library = 'library';
}
if(\class_exists($classname))
{
return self::$instance->array[$classname];
}
if(! \class_exists($classname))
{
$path = $classname.'.class.php';
require_once(self::$instance->library .'/' . $path);
self::$instance->array[$classname]=$path;
}
return self::$instance->array[$classname];
}
final public static function Load($classname)
{
if(isset(self::$instance->array[$classname]))
{
self::$instance->use = new $classname;
}
elseif(!isset(self::$instance->array[$classname]))
{
throw new SystemException('Fatal Error: Class' . $classname . 'doesn\'t Exists Class Must Be A Registered Class' );
}
return self::$instance->use;
}
}
namespace Loader;
class SystemException extends \Exception
{
public function __construct($message, $code = 0, \Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
?>
Usage Example....
<?php
// using namespace Loader\Registry.
use Loader\Registry;
// getting the registry.
require_once('registry.class.php');
//setting the Registry instance and access varibale to load and set classes.
$System = Registry::Init();
//Setting Class Output into the registry.
//optional Regsitry pram: you can set the directory as the second value of registerclass.
//$system->RegisterClass('testclass' , 'testdirectory');
// this will attempt to load clases out of the new directory.
$System->RegisterClass('output');
// try to load and use class functions.
try
{
//Load the class output for use.
$System->Load('output');
//using method Display from out.
$System->use->Display('josh');
}
// catch any systemexceptions
catch(\SystemException $e)
{
echo $e->getMessage().'-'. $e->getLine() .'-'. $e->getCode();
}
?>