public static function factory($namespace, $forceCreate = false) { // Singletons static $instances; if (isset($instances[$namespace])) { return $instances[$namespace]; } // Check if mongo collection exists $name = self::NAMESPACE_PREFIX . $namespace; if (!$forceCreate && !self::hasCollection($name)) { throw new NotFoundException("Missing namespace {$namespace} at db"); } // Create a mongo collection instance $instance = new self(); $instance->setCollectionName($name); // Store it for future $instances[$namespace] = $instance; return $instance; }