In Symfony you can inject services into the controller and use them. I get the suggestion that the properties can be readonly. Please see the follow two examples:
class ExampleAController
{
public function __construct(
private FooService $fooService,
private BarService $barService,
){}
}
class ExampleBController
{
public function __construct(
private readonly FooService $fooService,
private readonly BarService $barService,
){}
}
Are there any realworld advantages/disadvantages to adding readonly here?
Adding readonly ensures it doesnt change and helps protect the code from unwanted changes, but in the case if a service, which often has its own dependencies, rewriting the property will be a lot or work and will not go unnoticed in any way.
To be clear, I’m specifically talking about services. If, for example, you inject a private readonly string $apiKey I can see somewhat of a benefit.