PHP Class Registry

Submitted by:Jhon Brain

Date added:13 April, 2013

Category:PHP

Load and store classes for easy use and re-usability using advanced PHP object storing.

Tags: php object

Code Snippet:

<?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";
}
}

?>
 
 

Comments