Examples of how to write singleton classes in PHP
This article mainly introduces examples of how to write singleton classes in PHP. This article directly gives code examples. Friends who need them can refer to them. Next
The single instance class in PHP is still very meaningful in terms of data exchange and memory saving. Write a simple example.
Class 1, single instance class itself:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class UTIL {
private static $instance;
public function get() {
if (!self::$instance) {
self::$instance = new UTIL();
}
return self::$instance;
}
public $number = 10;
public function change($num) {
$this->number = $num;
}
public function getNum() {
return $this->number;
}
}
|
1
2
3
1
2
3
4
5
6
7
8
9
10
11
12
|
class SINGLEA {
private $numInst;
function __construct() {
$this->numInst = UTIL::get();
}
public function change($num) {
$this->numInst->change($num);
}
public function getNum() {
return $this->numInst->getNum();
}
}
|
4
5
1
2
3
4
5
6
7
8
9
10
11
12
|
class SINGLEB {
private $numInst;
function __construct() {
$this->numInst = UTIL::get();
}
public function change($num) {
$this->numInst->change($num);
}
public function getNum() {
return $this->numInst->getNum();
}
}
|
6
7
8
1
2
3
4
5
6
7
8
|
$instA = new SINGLEA();
$instA->change(100);
var_dump('SINGLEA CHANGED: ');
var_dump($instA->getNum());
$instB = new SINGLEB();
$instB->change(-510);
var_dump('SINGLEB CHANGED: ');
var_dump($instB->getNum());
|
9
10
1
2
3
4
|
string'SINGLEA CHANGED: ' (length=17)
int110
string'SINGLEB CHANGED: ' (length=17)
int-400
|
11
12
13
14
15
16
|
class UTIL {
private static $instance;
public function get() {
if (!self::$instance) {
self::$instance = new UTIL();
}
return self::$instance;
}
public $number = 10;
public function change($num) {
$this->number = $num;
}
public function getNum() {
return $this->number;
}
}
|
Class 2, application class using the aforementioned single instance class:
?
1
2
3
4
5
6
7
8
9
10
11
12
|
class SINGLEA {
private $numInst;
function __construct() {
$this->numInst = UTIL::get();
}
public function change($num) {
$this->numInst->change($num);
}
public function getNum() {
return $this->numInst->getNum();
}
}
|
Category 3, similar type 2:
?
1
2
3
4
5
6
7
8
9
10
11
12
|
class SINGLEB {
private $numInst;
function __construct() {
$this->numInst = UTIL::get();
}
public function change($num) {
$this->numInst->change($num);
}
public function getNum() {
return $this->numInst->getNum();
}
}
|
The last place is the call:
?
1
2
3
4
5
6
7
8
|
$instA = new SINGLEA();
$instA->change(100);
var_dump('SINGLEA CHANGED: ');
var_dump($instA->getNum());
$instB = new SINGLEB();
$instB->change(-510);
var_dump('SINGLEB CHANGED: ');
var_dump($instB->getNum());
|
The final display result:
?
1
2
3
4
|
string'SINGLEA CHANGED: ' (length=17)
int110
string'SINGLEB CHANGED: ' (length=17)
int-400
|
http://www.bkjia.com/PHPjc/1021558.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1021558.htmlTechArticleExamples of how to write singleton classes in PHP This article mainly introduces examples of how to write singleton classes in PHP. This article Code examples are given directly. Friends in need can refer to the single instance class in PHP for calculation...
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