php单例模式实现方法分析

yipeiwu_com6年前PHP代码库

本文实例讲述了php单例模式实现方法。分享给大家供大家参考。具体如下:

<?php
/**
 * @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();

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP魔术方法之__call与__callStatic使用方法

核心代码 //魔术方法__call /* $method 获得方法名 $arg 获得方法的参数集合 */ class Human { private function t(...

PHP 图像尺寸调整代码

复制代码 代码如下: /********************** *@filename - path to the image *@tmpname - temporary path...

使用JSON实现数据的跨域传输的php代码

后台profile.php代码: 复制代码 代码如下: <?php $arr = array( 'firstname' => iconv('gb2312', 'utf-8',...

PHP获取文件夹内文件数的方法

本文实例讲述了PHP获取文件夹内文件数的方法。分享给大家供大家参考。具体实现方法如下: function getfilecounts($ff){ $dir = './'.$ff;...

PHP编码规范的深入探讨

缩进与空白字符(Indenting and Whitespace)使用 2 个空格而不使用 tab 键进行代码缩进(notepad++, Eclipse 等编辑器均支持此项配置);行尾不...