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简单读取xml文件的方法示例

本文实例讲述了PHP简单读取xml文件的方法。分享给大家供大家参考,具体如下: 我将软件版本更新中的版本号等数据信息存放在xml文件中,使用时将版本信息读取出来。 xml文件内容如下:...

Admin generator, filters and I18n

Three easy steps 1) configure function Add an input for each field you want to include in you...

PHP获取汉字笔画数功能【测试可用】

本文实例讲述了PHP获取汉字笔画数功能。分享给大家供大家参考,具体如下: 无意中看到这么个东西,用PHP得到汉字的笔画数。以类的方式实现,有那么点意思,先留下了。 <?...

PHP基于DOM创建xml文档的方法示例

本文实例讲述了PHP基于DOM创建xml文档的方法。分享给大家供大家参考,具体如下: DOM创建xml文档 用dom创建如下文档: <booklist>   <bo...

php调用方法mssql_fetch_row、mssql_fetch_array、mssql_fetch_assoc和mssql_fetch_objcect读取数据的区别

方法名:mssql_fetch_row() 测试: 复制代码 代码如下: require 'dbconn.php'; $sql = 'select * from _Test'; $que...