Home > Article > Backend Development > Principle and implementation method of PHP singleton mode
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('hhhh'); echo '$s:'.$s->getName(); unset($s); $m = Single::init(); echo '$m:'.$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!