Aer/tests/singleton_test.aer

33 lines
544 B
Plaintext
Raw Normal View History

2018-08-10 08:51:38 +02:00
final class Test {
private $value;
private function __construct() {
}
/* This is singleton */
public function getInstance() {
static object $instance;
2018-08-10 08:51:38 +02:00
if(!$instance) {
$instance = new Test();
}
return $instance;
}
public function get() {
return $this->value;
}
2018-09-14 21:32:08 +02:00
public function set(int $value = 0) {
2018-08-10 08:51:38 +02:00
$this->value = $value;
}
}
final class Program {
public function 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 */