5 * Basis class for singleton classes.
7 * @author Alexander Ebert
8 * @copyright 2001-2016 WoltLab GmbH
9 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
10 * @package com.woltlab.wcf
12 * @category Community Framework
14 abstract class SingletonFactory
{
17 * @var array<SingletonFactory>
19 protected static $__singletonObjects = array();
22 * Singletons do not support a public constructor. Override init() if
23 * your class needs to initialize components on creation.
25 protected final function __construct() {
30 * Called within __construct(), override if neccessary.
32 protected function init() { }
35 * Object cloning is disallowed.
37 protected final function __clone() { }
40 * Object serializing is disallowed.
42 public final function __sleep() {
43 throw new \
Exception('Serializing of Singletons is not allowed');
47 * Returns an unique instance of current child class.
49 * @return \dns\system\SingletonFactory
51 public static final function getInstance() {
52 $className = get_called_class();
53 if (!array_key_exists($className, self
::$__singletonObjects)) {
54 self
::$__singletonObjects[$className] = null;
55 self
::$__singletonObjects[$className] = new $className();
57 else if (self
::$__singletonObjects[$className] === null) {
58 throw new \
Exception("Infinite loop detected while trying to retrieve object for '".$className."'");
61 return self
::$__singletonObjects[$className];
65 * Returns whether this singleton is already initialized.
69 public static final function isInitialized() {
70 $className = get_called_class();
72 return isset(self
::$__singletonObjects[$className]);