Back to CMSApplication class

Method getInstance

public static \Joomla\CMS\Application\CmsApplication
getInstance
(mixed $name = null, mixed $prefix = '\\JApplication', \Joomla\DI\Container $container = null)
Returns a reference to the global CmsApplication object, only creating it if it doesn't already exist.
Parameters
  • string $name The name (optional) of the CmsApplication class to instantiate.
  • string $prefix The class name prefix of the object.
  • \Joomla\DI\Container $container An optional dependency injection container to inject into the application.
Returns
  • \Joomla\CMS\Application\CmsApplication
Since
  • 3.2
Deprecated
  • 5.0
-
  • \RuntimeException

Method getInstance - Source code

/**
 * Returns a reference to the global CmsApplication object, only creating it if it doesn't already exist.
 *
 * This method must be invoked as: $web = CmsApplication::getInstance();
 *
 * @param   string     $name       The name (optional) of the CmsApplication class to instantiate.
 * @param   string     $prefix     The class name prefix of the object.
 * @param   Container  $container  An optional dependency injection container to inject into the application.
 *
 * @return  CmsApplication
 *
 * @since       3.2
 * @throws      \RuntimeException
 * @deprecated  5.0 Use \Joomla\CMS\Factory::getContainer()->get($name) instead
 */
public static function getInstance($name = null, $prefix = '\\JApplication', Container $container = null)
{
    if (empty(static::$instances[$name])) {
        // Create a CmsApplication object.
        $classname = $prefix . ucfirst($name);
        if (!$container) {
            $container = Factory::getContainer();
        }
        if ($container->has($classname)) {
            static::$instances[$name] = $container->get($classname);
        } elseif (class_exists($classname)) {
            // @todo This creates an implicit hard requirement on the ApplicationCms constructor
            static::$instances[$name] = new $classname(null, null, null, $container);
        } else {
            throw new \RuntimeException(Text::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD', $name), 500);
        }
        static::$instances[$name]->loadIdentity(Factory::getUser());
    }
    return static::$instances[$name];
}