You are here

class ExampleClassWithDependencyInjection in Coder 8.3.x

Same name and namespace in other branches
  1. 8.3 tests/DrupalPractice/Objects/drupal8/ExampleClassWithDependencyInjection.php \Drupal\testmodule\ExampleClassWithDependencyInjection

A class which is not a service but has access to the container.

Hierarchy

Expanded class hierarchy of ExampleClassWithDependencyInjection

File

tests/DrupalPractice/Objects/drupal8/ExampleClassWithDependencyInjection.php, line 11

Namespace

Drupal\testmodule
View source
class ExampleClassWithDependencyInjection implements ContainerInjectionInterface {

  /**
   * Using \Drupal here is not allowed, it should be injected instead.
   */
  public function test() {
    return \Drupal::configFactory();
  }

  /**
   * Loading nodes directly is not allowed, we should use an injected service.
   */
  public function test2() {
    return Node::load(1);
  }

  /**
   * Global function should not be used, we should use an injected service.
   */
  public function test3() {
    return format_date(time());
  }

  /**
   * t() should not be used, instead we should use the StringTranslationTrait.
   */
  public function test4() {
    return t('Test');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContainerInjectionInterface::create public static function Instantiates a new instance of this class. 75
ExampleClassWithDependencyInjection::test public function Using \Drupal here is not allowed, it should be injected instead.
ExampleClassWithDependencyInjection::test2 public function Loading nodes directly is not allowed, we should use an injected service.
ExampleClassWithDependencyInjection::test3 public function Global function should not be used, we should use an injected service.
ExampleClassWithDependencyInjection::test4 public function t() should not be used, instead we should use the StringTranslationTrait.