xref: /PHP-8.1/tests/classes/singleton_001.phpt (revision f8d79582)
1--TEST--
2ZE2 singleton
3--FILE--
4<?php
5
6class Counter {
7    private $counter = 0;
8
9    function increment_and_print() {
10        echo ++$this->counter;
11        echo "\n";
12    }
13}
14
15
16class SingletonCounter {
17    private static $m_instance = NULL;
18
19    static function Instance() {
20        if (self::$m_instance == NULL) {
21            self::$m_instance = new Counter();
22        }
23        return self::$m_instance;
24    }
25}
26
27SingletonCounter::Instance()->increment_and_print();
28SingletonCounter::Instance()->increment_and_print();
29SingletonCounter::Instance()->increment_and_print();
30
31?>
32--EXPECT--
331
342
353
36