/**
* Singleton mode implementation class -->ArrayAccess (array access) interface
*
* @author flyer0126
* @since 2012/4/27
*/
class Single{
private $name;
private static $_Instance = null;
private function __construct()
{
}
static function load()
{
if(null == self::$_Instance)
{
self::$_Instance = new Single();
}
return self::$_Instance;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
$s = Single::load();
$s->setName("jack");
echo $s->getName(); //jack
// 调整一下(继承ArrayAccess && 实现4个方法)
class Single implements ArrayAccess
{
private $name;
private static $_Instance = null;
private function __construct()
{
}
static function load()
{
if(null == self::$_Instance)
{
self::$_Instance = new Single();
}
return self::$_Instance;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
/**
to ’ s
’ ’ s ’ s ’ s to 1's ’ out out out out off out out through through ‐ to ‐ ‐ ‐ ‐ ‐ Value
*/
public function offsetSet($offset, $value)
{
if (is_null($offset))
{
$this->container[] = $value;
}
else
{
$this->container[$offset] = $value;
}
}
public function offsetGet($offset) {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
public function offsetExists($offset) {
return isset($this->container[$offset]);
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
}
}
$s = Single::load();$s->setName("jack");
print_r($s);
/**
Single Object
(
[name:Single:private] => jack
[container] => Array
(
[name] => mike
)
)**/