php单件模式实现代码演示
<?php
/**
* @link https://github.com/MaGuowei
* @copyright 2013 maguowei.com
* @author Ma Guowei <imaguowei@gmail.com>
*/
/**
* 单例模式
* Class Single
*/
class Single
{
private $name;
private static $single;
private function __construct()
{
}
public static function init()
{
if(empty(self::$single))
{
self::$single = new Single();
}
return self::$single;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
$s = Single::init();
$s->setName('hhhh');
echo '$s:'.$s->getName();
unset($s);
$m = Single::init();
echo '$m:'.$m->getName();
