Service Method Calls and Setter Injection

Tip

ヒント

If you're using autowiring, you can use #[Required] to automatically configure method calls.

自動配線を使用している場合は、 #[Required] を使用してメソッド呼び出しを自動的に構成できます。

Usually, you'll want to inject your dependencies via the constructor. But sometimes, especially if a dependency is optional, you may want to use "setter injection". For example:

通常、コンストラクターを介して依存関係を注入する必要があります。ただし、特に依存関係がオプションの場合は、「セッター注入」を使用したい場合があります。例えば:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// src/Service/MessageGenerator.php
namespace App\Service;

use Psr\Log\LoggerInterface;

class MessageGenerator
{
    private $logger;

    public function setLogger(LoggerInterface $logger): void
    {
        $this->logger = $logger;
    }

    // ...
}

To configure the container to call the setLogger method, use the calls key:

setLogger メソッドを呼び出すようにコンテナーを構成するには、calls キーを使用します。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
# config/services.yaml
services:
    App\Service\MessageGenerator:
        # ...
        calls:
            - setLogger: ['@logger']

To provide immutable services, some classes implement immutable setters. Such setters return a new instance of the configured class instead of mutating the object they were called on:

不変のサービスを提供するために、一部のクラスは不変のセッターを実装しています。このようなセッターは、呼び出されたオブジェクトを変更する代わりに、構成されたクラスの新しいインスタンスを返します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// src/Service/MessageGenerator.php
namespace App\Service;

use Psr\Log\LoggerInterface;

class MessageGenerator
{
    private $logger;

    public function withLogger(LoggerInterface $logger): self
    {
        $new = clone $this;
        $new->logger = $logger;

        return $new;
    }

    // ...
}

Because the method returns a separate cloned instance, configuring such a service means using the return value of the wither method ($service = $service->withLogger($logger);). The configuration to tell the container it should do so would be like:

メソッドは別の複製されたインスタンスを返すため、そのようなサービスを構成することは wither メソッドの戻り値を使用することを意味します ($service = $service->withLogger($logger);)。コンテナーにそうする必要があることを伝える構成は次のようになります。 :
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
# config/services.yaml
services:
    App\Service\MessageGenerator:
        # ...
        calls:
            - withLogger: !returns_clone ['@logger']

Tip

ヒント

If autowire is enabled, you can also use attributes; with the previous example it would be:

自動配線が有効になっている場合は、属性も使用できます。前の例では、次のようになります。
1
2
3
4
5
6
7
8
9
10
11
/**
 * @return static
 */
#[Required]
public function withLogger(LoggerInterface $logger)
{
    $new = clone $this;
    $new->logger = $logger;

    return $new;
}

You can also leverage the PHP 8 static return type instead of the @return static annotation. If you don't want a method with a PHP 8 static return type and a #[Required] attribute to behave as a wither, you can add a @return $this annotation to disable the returns clone feature.

@return 静的アノテーションの代わりに、PHP 8 静的戻り型を利用することもできます。 PHP 8 の静的な戻り値の型と #[Required] 属性を持つメソッドをウィザーとして動作させたくない場合は、@return $this アノテーションを追加して戻りクローン機能を無効にすることができます。