Home > Backend Development > PHP Tutorial > PHP design pattern Singleton (single case mode)_PHP tutorial

PHP design pattern Singleton (single case mode)_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:27:29
Original
906 people have browsed it

Copy code The code is as follows:

/**
* Singleton pattern
*
* Ensures that a class has only one instance and provides a global access point to access it
*
*/
class Singleton
{
static private $_instance = null;

private function __construct()
{
}

static public function getInstance()
{
if(is_null(self::$_instance)) {
self::$_instance = new Singleton();
}
return self::$_instance;
}

public function display()
{
echo "it is a singlton class function";
}
}

// $obj = new Singleton(); // Statement Unable to succeed
$obj = Singleton::getInstance();
var_dump($obj);
$obj->display();

$obj1 = Singleton::getInstance( );
var_dump(($obj === $obj1));

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323802.htmlTechArticleCopy the code as follows: ?php /*** Singleton pattern * * Ensures that a class has only one instance and provides a global access point to access it **/ class Singleton { static private $_instance.. .
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template