class Registry
{
/*
* @var array Holds all the main objects in an array a greater scope access
* @access private
*/
private static $objects = array();
/**
* Add's an object into the the global
* @param string $name
* @param string $object
* @return bool
*/
public static function add($name,$object)
{
self::$objects[$name] = $object;
return true;
}
/*
* Get's an object out of the registry
* @param string $name
* @return object on success, false on failure
*/
public static function get($name)
{ if(isset(self::$objects[$name]))
{
return self::$objects[$name];
}
return false;
}
/**
* Removes an object out of Registry (Hardly used)
* @param string $name
* @return bool
*/
static function remove($name)
{
unset(self::$objects[$name]);
return true;
}
/**
* Checks if an object is stored within the registry
* @param string $name
* @return bool
*/
static function is_set($name)
{
return isset(self::$objects[$name]);
}
}`
The you can use like so.
require_once 'classes/registry.class.php';
require_once 'classes/database/pdo.class.php';
Registry::set('Database',new Database);
Then anywhere in your Application you can do like so:
function addPost(String $post,Int $id)
{
$statement = Registry::get('Database')->prepare('INSERT INTO posts VALUES(:d,:p)');
$statement->bindValue(':p',$id,PDO::PARAM_INT);
$statement->bindValue(':p',$post);
}
This will give you a much Larger scope!