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程序设计有所帮助。

相关文章

探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解

Smarty中如何获取数组的长度 前提假设:分配了一个数组array给Smarty,假设Smarty的分界符为'{' 和'}'。在很多资料上都看到,在Smarty中要求数组的长度时,可以...

详解php的魔术方法__get()和__set()使用介绍

先看看php官方文档的解释: __set() is run when writing data to inaccessible properties. __get() is utiliz...

比较discuz和ecshop的截取字符串函数php版

下面先给出两个版本函数的源代码以及简单测试,最后我会给出一个实用性更强的字符串截取函数。需要注意的是:这里讨论的字符串截取问题都是针对UTF-8编码的中文字符串。 discuz版本 复制...

判断Keep-Alive模式的HTTP请求的结束的实现代码

所以根据EOF就可判断一次请求的结束,下面的代码(PHP)很常见: 复制代码 代码如下: // $fp是由fsockopen()产生的句柄 while(!feof($fp)) { ech...

分享php分页的功能模块

分享php分页的功能模块

先贴张图看看效果 在贴一下代码吧 <?php $localhost = "localhost"; $username = "root"; $password =...