Principle and implementation method of PHP singleton mode

墨辰丷
Release: 2023-03-31 19:12:01
Original
2909 people have browsed it

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template