Skip to content

Instantly share code, notes, and snippets.

@app2641
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save app2641/9088401 to your computer and use it in GitHub Desktop.
Save app2641/9088401 to your computer and use it in GitHub Desktop.
Zend_Regisry もどきのクラス
<?php
class Registry extends \ArrayObject
{
/**
* @var Registry
**/
private static $instance;
/**
* Registryインスタンスを返す
*
* @return Registry
**/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new Registry();
}
return self::$instance;
}
/**
* 格納物を取得する
*
* @param String $index 格納キー
* @return Object
**/
public static function get ($index)
{
$instance = self::getInstance();
if (! $instance->ifKeyExists($index)) {
throw new \Exception($index.' は登録されていません!');
}
return $instance->offsetGet($index);
}
/**
* 格納する
*
* @param String $index 格納キー
* @param Object $value 格納物
* @return void
**/
public static function set ($index, $value)
{
$instance = self::getInstance();
$instance->offsetSet($index, $value);
}
/**
* 指定格納キーが既に登録されているか
*
* @param String $index 格納キー
* @return boolean
**/
public function ifKeyExists ($index)
{
return array_key_exists($index, $this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment