分享一个php类注册管理

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

}

?>

希望能帮助有需要的朋友 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值