龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

单件模式 php单件模式实现代码演示

时间:2014-07-08 02:35来源:网络整理 作者:网络 点击:
分享到:
php单件模式实现代码演示 [代码片段(53行)]
<?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();

精彩图集

赞助商链接