singles php design pattern Singleton singleton pattern

WBOY
Release: 2016-07-29 08:45:50
Original
1052 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(); // The declaration cannot succeed
$obj = Singleton::getInstance();
var_dump($obj );
$obj->display();
$obj1 = Singleton::getInstance();
var_dump(($obj === $obj1));

The above introduces the singles PHP design pattern Singleton singleton mode, including the content of singles. I hope it will be helpful to friends who are interested in PHP tutorials.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!