Last active
August 29, 2015 13:56
-
-
Save app2641/9088401 to your computer and use it in GitHub Desktop.
Zend_Regisry もどきのクラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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