<?php * @copyright Copyright (c) 2010纯然科技 class Cr_Registry extends ArrayObject { private static $_instanceClassName = 'Cr_Registry'; private static $_instance = null; final public static function getInstance() { if (self::$_instance === null) { self::init(); } return self::$_instance; } public static function setInstance(cr_Registry $registry) { if (self::$_instance !== null) { throw new Exception('Registry is already initialized'); } self::setClassName(get_class($registry)); self::$_instance = $registry; } protected static function init() { self::setInstance(new self::$_instanceClassName()); } public static function setClassName($registryClassName = 'Cr_Registry') { if (self::$_instance !== null) { throw new Exception('Registry is already initialized'); } if (!is_string($registryClassName)) { throw new Exception("Argument is not a class name"); } self::$_instanceClassName = $registryClassName; } public static function _unsetInstance() { self::$_instance = null; } public static function get($index) { $instance = self::getInstance(); if (!$instance->offsetExists($index)) { return ; //throw new Exception("No entry is registered for key '$index'"); } return $instance->offsetGet($index); } public static function set($index, $value) { $instance = self::getInstance(); $instance->offsetSet($index, $value); } public static function isRegistered($index) { if (self::$_instance === null) { return false; } return self::$_instance->offsetExists($index); } public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS) { parent::__construct($array, $flags); } public function offsetExists($index) { return array_key_exists($index, $this); } } ?>
希望能帮助有需要的朋友