1: <?php
2: /**
3: * @filesource Kotchasan/Singleton.php
4: *
5: * @copyright 2016 Goragod.com
6: * @license http://www.kotchasan.com/license/
7: *
8: * @see http://www.kotchasan.com/
9: */
10:
11: namespace Kotchasan;
12:
13: /**
14: * Singleton base class.
15: *
16: * @author Goragod Wiriya <admin@goragod.com>
17: *
18: * @since 1.0
19: */
20: abstract class Singleton
21: {
22: /**
23: * @var Singleton สำหรับเรียกใช้ class นี้เพียงครั้งเดียวเท่านั้น
24: */
25: private static $instance = null;
26:
27: /**
28: * เรียกใช้งาน Class แบบสามารถเรียกได้ครั้งเดียวเท่านั้น.
29: *
30: * @return \static
31: */
32: public static function &getInstance()
33: {
34: if (null === static::$instance) {
35: static::$instance = new static();
36: }
37:
38: return static::$instance;
39: }
40:
41: /**
42: * method เรียกเมื่อมีการโหลด Class.
43: */
44: abstract protected function init();
45:
46: private function __clone()
47: {
48: // do nothing
49: }
50:
51: private function __construct()
52: {
53: // initial class
54: static::init();
55: }
56:
57: private function __wakeup()
58: {
59: // do nothing
60: }
61: }
62: