Aer/tests/singleton_test.aer

38 lines
645 B
Plaintext
Raw Permalink Normal View History

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() {
2019-06-29 16:04:15 +02:00
print('Constructor called.' + "\n");
}
private void __destruct() {
print('Destructor called.' + "\n");
2018-08-10 08:51:38 +02:00
}
/* This is singleton */
2019-05-08 11:05:36 +02:00
public static object getInstance() {
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;
}
}
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 */