2018-08-10 08:51:38 +02:00
|
|
|
final class Test {
|
2019-03-17 19:48:52 +01:00
|
|
|
private int $value;
|
2018-08-10 08:51:38 +02:00
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
private void __construct() {
|
2018-08-10 08:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is singleton */
|
2019-05-08 11:05:36 +02:00
|
|
|
public static object getInstance() {
|
2018-09-21 00:21:15 +02:00
|
|
|
static object $instance;
|
2018-08-10 08:51:38 +02:00
|
|
|
if(!$instance) {
|
|
|
|
$instance = new Test();
|
|
|
|
}
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
public int get() {
|
2018-08-10 08:51:38 +02:00
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
public int set(int $value = 0) {
|
2018-08-10 08:51:38 +02:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 12:52:35 +02:00
|
|
|
final class Program {
|
2019-03-17 19:48:52 +01:00
|
|
|
public void main() {
|
2018-09-23 17:51:47 +02:00
|
|
|
object $testA = Test::getInstance();
|
2018-08-10 08:51:38 +02:00
|
|
|
$testA->set(5);
|
2018-09-23 17:51:47 +02:00
|
|
|
object $testB = Test::getInstance();
|
2018-08-10 08:51:38 +02:00
|
|
|
var_dump($testB->get());
|
|
|
|
}
|
|
|
|
} /* class */
|