You are here

class ServiceWithDependency in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/new_dependency_test/src/ServiceWithDependency.php \Drupal\new_dependency_test\ServiceWithDependency
  2. 10 core/modules/system/tests/modules/new_dependency_test/src/ServiceWithDependency.php \Drupal\new_dependency_test\ServiceWithDependency

Service that gets the other service of the same module injected.

This service indirectly depends on a not-yet-defined service.

Hierarchy

Expanded class hierarchy of ServiceWithDependency

1 string reference to 'ServiceWithDependency'
new_dependency_test.services.yml in core/modules/system/tests/modules/new_dependency_test/new_dependency_test.services.yml
core/modules/system/tests/modules/new_dependency_test/new_dependency_test.services.yml
5 services use ServiceWithDependency
new_dependency_test.alias_dependency in core/modules/system/tests/modules/new_dependency_test/new_dependency_test.services.yml
Drupal\new_dependency_test\ServiceWithDependency
new_dependency_test.alias_dependency2 in core/modules/system/tests/modules/new_dependency_test/new_dependency_test.services.yml
Drupal\new_dependency_test\ServiceWithDependency
new_dependency_test.hard_dependency in core/modules/system/tests/modules/new_dependency_test/new_dependency_test.services.yml
Drupal\new_dependency_test\ServiceWithDependency
new_dependency_test.optional_dependency in core/modules/system/tests/modules/new_dependency_test/new_dependency_test.services.yml
Drupal\new_dependency_test\ServiceWithDependency
new_dependency_test.recursion in core/modules/system/tests/modules/new_dependency_test/new_dependency_test.services.yml
Drupal\new_dependency_test\ServiceWithDependency

File

core/modules/system/tests/modules/new_dependency_test/src/ServiceWithDependency.php, line 10

Namespace

Drupal\new_dependency_test
View source
class ServiceWithDependency {

  /**
   * The injected service.
   *
   * @var \Drupal\new_dependency_test\InjectedService
   */
  protected $service;

  /**
   * ServiceWithDependency constructor.
   *
   * @param \Drupal\new_dependency_test\InjectedService|null $service
   *   The service of the same module which has the new dependency.
   */
  public function __construct(InjectedService $service = NULL) {
    $this->service = $service;
  }

  /**
   * Gets a greeting from the injected service and adds to it.
   *
   * @return string
   *   The greeting.
   */
  public function greet() {
    if (isset($this->service)) {
      return $this->service
        ->greet() . ' World';
    }
    return 'Sorry, no service.';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServiceWithDependency::$service protected property The injected service.
ServiceWithDependency::greet public function Gets a greeting from the injected service and adds to it.
ServiceWithDependency::__construct public function ServiceWithDependency constructor.