public function Container::get

Gets a service.

If a service is defined both through a set() method and with a get{$id}Service() method, the former has always precedence.

@api

Parameters

string $id The service identifier:

integer $invalidBehavior The behavior when the service does not exist:

Return value

object The associated service

Throws

InvalidArgumentException if the service is not defined

Overrides ContainerInterface::get

See also

Reference

6 calls to Container::get()
ContainerBuilder::get in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php
Gets a service.
ProjectServiceContainer::getAliasForFooService in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php
Gets the alias_for_foo service alias.
ProjectServiceContainer::getBarService in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php
Gets the 'bar' service.
ProjectServiceContainer::getCircularService in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
ProjectServiceContainer::getFooService in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php
Gets the 'foo' service.

... See full list

1 method overrides Container::get()
ContainerBuilder::get in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php
Gets a service.

File

drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Container.php, line 237

Class

Container
Container is a dependency injection container.

Namespace

Symfony\Component\DependencyInjection

Code

public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {
  $id = strtolower($id);
  if (isset($this->services[$id])) {
    return $this->services[$id];
  }
  if (isset($this->loading[$id])) {
    throw new ServiceCircularReferenceException($id, array_keys($this->loading));
  }
  if (method_exists($this, $method = 'get' . strtr($id, array(
    '_' => '',
    '.' => '_',
  )) . 'Service')) {
    $this->loading[$id] = true;
    try {
      $service = $this
        ->{$method}();
    } catch (\Exception $e) {
      unset($this->loading[$id]);
      throw $e;
    }
    unset($this->loading[$id]);
    return $service;
  }
  if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
    throw new ServiceNotFoundException($id);
  }
}