Skip to content

Instantly share code, notes, and snippets.

@a-suenami
Last active December 10, 2015 08:58
Show Gist options
  • Save a-suenami/4411372 to your computer and use it in GitHub Desktop.
Save a-suenami/4411372 to your computer and use it in GitHub Desktop.
ENUM値をオブジェクトにしたい。
<?php
class Status
{
private $value;
private static $instances = array();
private function __construct($value)
{
$this->value = $value;
}
private function _getCacheKey($value)
{
return sha1('status_'.$value);
}
public static function getInstance($value)
{
$key = $this->_getCacheKey($value);
$cached_instance = apc_fetch($key);
if ($cached_instance) return $cached_instance;
if (isset($this->instances[$value])) return $this->instances[$value];
$instance = new Status($value);
$this->instances[$value] = $instance;
apc_add($key, $instance);
return $instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment