`SingletonFactory::getInstance()` now uses `isset` instead of `array_key_exists`
authorAlexander Ebert <ebert@woltlab.com>
Wed, 13 Mar 2019 12:15:37 +0000 (13:15 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 13 Mar 2019 12:15:37 +0000 (13:15 +0100)
The behavior remains unchanged for both valid calls, as well as for detecting infinite loops due to cross references.

wcfsetup/install/files/lib/system/SingletonFactory.class.php

index 95d335b9005d2863476d6c2529135993399c5619..8e0a8ee5b13557b348288a77c531ae9e72de2e62 100644 (file)
@@ -49,13 +49,15 @@ abstract class SingletonFactory {
         * @throws      SystemException
         */
        public static final function getInstance() {
-               $className = get_called_class();
-               if (!array_key_exists($className, self::$__singletonObjects)) {
-                       self::$__singletonObjects[$className] = null;
+               $className = static::class;
+               if (!isset(self::$__singletonObjects[$className])) {
+                       // The previously used `null` value forced us into using `array_key_exists()` which has a bad performance,
+                       // especially with the growing list of derived classes that are used today. Saves a few ms on every request.
+                       self::$__singletonObjects[$className] = false;
                        self::$__singletonObjects[$className] = new $className();
                }
-               else if (self::$__singletonObjects[$className] === null) {
-                       throw new SystemException("Infinite loop detected while trying to retrieve object for '".$className."'");
+               else if (self::$__singletonObjects[$className] === false) {
+                       throw new SystemException("Infinite loop detected while trying to retrieve object for '" . $className . "'");
                }
                
                return self::$__singletonObjects[$className];