Scripts
Registry classe
Misschien is het wat overbodig voor de mensen die OOP programmeren, maar ik post em toch, kan handig zijn voor beginners. Deze classe dient als registry classe voor je webapplicatie. Om het registry pattern toe te voegen dus. Is te gebruiken als array.
registry-classe
<?php
class Registry implements ArrayAccess, Iterator, Countable {
private $globals;
// Magic methods
public function __construct() {
$this->globals = array();
}
public function __destruct() {
}
public function __get( $key ) {
return $this->offsetGet ( $key );
}
public function __set ( $key, $value ) {
$this->offsetSet ( $key, $value );
}
public static function getInstance() {
static $instance;
if (! is_object ( $instance ) ) {
$obj = __CLASS__;
$instance = new $obj;
}
return $instance;
}
// CLASS METHODS
// Voegt een nieuw element toe aan de $this->globals array
public function add ( $global, $key = '' ) {
if ( strlen ($key) > 0 ) {
if ( ! $this->offsetExists ( $key ) ) {
$this->globals [ $key ] = &$global;
} else {
throw new Exception("Duplicate etry found");
}
} else {
$this->globals[] = &$global;
}
}
// Verwijder een element uit de globals array
public function delete ( $key ) {
$this->offsetUnset ( $key );
}
// Verwijder alle elementen uit de globals array
public function clear () {
$this->globals = array();
}
// Geef het een bepaald element terug
public function get ( $offset ) {
return $this->offsetGet ( $offset );
}
// IMPLEMENTATIONS
//-- ArrayAccess
public function offsetExists ( $offset ) {
return isset ( $this->globals [ $offset ] );
}
public function offsetGet ( $offset ) {
if ( $this->offsetExists ( $offset ) ) {
return $this->globals [ $offset ];
} else {
throw new RangeException("Index out of bounds");
}
}
public function offsetSet ( $offset, $value ) {
if ( $this->offsetExists ( $offset ) ) {
$this->globals [ $offset ] = &$value;
} else {
throw new RangeException("Index out of bounds");
}
}
public function offsetUnset ( $offset ) {
if ( $this->offsetExists ( $offset ) ) {
unset ( $this->globals [ $offset ] );
} else {
throw new RangeException("Index out of bounds");
}
}
//-- Iterator
public function current() {
return current ( $this->globals );
}
public function next() {
return next ( $this->globals );
}
public function key() {
return key ( $this->globals );
}
public function rewind() {
reset ( $this->globals );
}
public function valid() {
return $this->current() !== false;
}
//-- Countable
public function count () {
return count ( $this->globals );
}
}
?>
Wat voorbeelden van hoe te gebruiken:
<?php
$reg = Registry::getInstance();
$reg->add ( $db, 'database' );
$reg->add ( $user, 'user' );
$reg->add ( $template, 'template' );
echo $reg->database;
echo $reg->user;
echo $reg['template'];
foreach ( $reg as $t ) {
echo $t;
}
class blaat {
private $db;
public function __construct() {
$this->db = Registry::getInstance()->database;
$this->user = Registry::getInstance()->get ('user');
}
public function blabla () {
$reg = Registry::getInstance();
$reg['db']->query();
}
}
?>
Reacties
0