Home  >  Article  >  Backend Development  >  Principle and implementation method of PHP singleton mode

Principle and implementation method of PHP singleton mode

墨辰丷
墨辰丷Original
2018-06-12 15:10:512908browse

This article mainly introduces the PHP singleton mode. The examples analyze the principles and implementation techniques of the singleton mode. It has certain reference value. Friends in need can refer to it.

The examples in this article describe the PHP singleton mode. Example pattern implementation method. The details are as follows:

<?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(&#39;hhhh&#39;);
echo &#39;$s:&#39;.$s->getName();
unset($s);
$m = Single::init();
echo &#39;$m:&#39;.$m->getName();

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Definition and usage of is_file() function in PHP

Concept of PHP timestamp and time zone

Brief description of PHP date function and how to get the set time

The above is the detailed content of Principle and implementation method of PHP singleton mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn