Aer/tests/singleton_test.aer

38 lines
645 B
Plaintext

final class Test {
private int $value;
private void __construct() {
print('Constructor called.' + "\n");
}
private void __destruct() {
print('Destructor called.' + "\n");
}
/* This is singleton */
public static object getInstance() {
static object $instance;
if(!$instance) {
$instance = new Test();
}
return $instance;
}
public int get() {
return $this->value;
}
public int set(int $value = 0) {
$this->value = $value;
}
}
final class Program {
public void main() {
object $testA = Test::getInstance();
$testA->set(5);
object $testB = Test::getInstance();
var_dump($testB->get());
}
} /* class */