Back to User class

Method getInstance

public static \Joomla\CMS\User\User
getInstance
(mixed $identifier = 0)
Returns the global User object, only creating it if it doesn't already exist.
Parameters
  • int $identifier The primary key of the user to load (optional).
Returns
  • \Joomla\CMS\User\User The User object.
Since
  • 1.7.0
Deprecated
  • 5.0
Class: User
Project: Joomla

Method getInstance - Source code

/**
 * Returns the global User object, only creating it if it doesn't already exist.
 *
 * @param   integer  $identifier  The primary key of the user to load (optional).
 *
 * @return  User  The User object.
 *
 * @since       1.7.0
 * @deprecated  5.0  Load the user service from the dependency injection container or via $app->getIdentity()
 */
public static function getInstance($identifier = 0)
{
    @trigger_error(sprintf('%1$s() is deprecated. Load the user from the dependency injection container or via %2$s::getApplication()->getIdentity().', __METHOD__, __CLASS__), E_USER_DEPRECATED);
    // Find the user id
    if (!is_numeric($identifier)) {
        return Factory::getContainer()->get(UserFactoryInterface::class)->loadUserByUsername($identifier);
    } else {
        $id = $identifier;
    }
    // If the $id is zero, just return an empty User.
    // Note: don't cache this user because it'll have a new ID on save!
    if ($id === 0) {
        return Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById($id);
    }
    // Check if the user ID is already cached.
    if (empty(self::$instances[$id])) {
        self::$instances[$id] = Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById($id);
    }
    return self::$instances[$id];
}