Nathan Nobbe wrote:
On Wed, Sep 24, 2008 at 6:28 AM, Richard Heyes <richard@xxxxxxx> wrote:
/me prefers singleton with __get and __set but each to their own :)
Do you mean registry?
could be, but it doesnt have to. when i think of a registry, i think of
something that stores a single instance of several classes in a structure
like an array, and then when asked by clients for an instance of a certain
class, it either gives them a handle to the object it has internally or
creates it first (if not yet created) then returns the handle. earlier in
this thread we were talking about using the singleton as a way to have
'custom superglobals' essentially, which imo, is different than a registry.
-nathan
Here's one of mine.. works sweet and gives access to all global level
variables anywhere (with read only option)..
<?php
/**
* LEVEL ZERO [lz] GLOBAL replacement object with read only option
*
* @author Nathan Rixham
* @version 2
*/
class lz {
private static $lz = array();
private static $stored = FALSE;
private static $readonly = TRUE;
public $variables;
private static function savelz(&$lz) {
if( !self::$stored ) {
self::$lz = $lz;
self::$stored = TRUE;
}
}
private static function &getlz() {
return self::$lz;
}
public function __construct( $variables = FALSE , $readonly=FALSE ) {
if( $variables !== FALSE && !self::$stored ) {
self::$readonly = $readonly;
self::savelz($this);
$this->variables = $variables['GLOBALS'];
unset( $this->variables['GLOBALS'] );
} elseif( self::$stored ) {
$this->variables = lz::getlz()->variables;
unset( $this->variables['lz'] );
}
}
public function __get( $name )
{
if(isset(self::$lz->variables[$name])) {
return self::$lz->variables[$name];
}
}
public function __set( $name , $value ) {
if( !self::$readonly ) {
$this->variables[$name] = $value;
}
}
}
# WRITE ACCESS VERSION
$lz = new lz( get_defined_vars() );
# READ ONLY VERSION
# $lz = new lz( get_defined_vars() , TRUE );
# EXAMPLE
$egg = 'shell';
function examplelz() {
$lz = new lz;
echo PHP_EOL . __FUNCTION__ . ' - BEFORE SET : ' . $lz->egg . PHP_EOL;
$lz->egg = 'new value';
echo PHP_EOL . __FUNCTION__ . ' - AFTER SET : ' . $lz->egg . PHP_EOL;
}
echo PHP_EOL . 'BEFORE FUNCTION : ' . $egg . PHP_EOL;
examplelz();
echo PHP_EOL . 'AFTER FUNCTION : ' . $egg . PHP_EOL;
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php