Answer the question
In order to leave comments, you need to log in
How to refactor classes with heterogeneous constructors?
The Symfony tag is listed as the question is about its Dependency Injection.
Project on Symfony 5.0.
How can you optimally refactor code that has several classes of the same child, but they have different constructors, while class creation is assigned to the factory?
I wanted to get away from disparate constructors, but this is only achieved by not injecting into constructors.
Conditional classes:
<?php
abstract class A {...}
class A1 extends A
{
public function __construct($a, $b, $c) {/*...*/}
}
class A2 extends A
{
public function __construct($a, $d, $e) {/*...*/}
}
class AN extends A
{
public function __construct($a) {/*...*/}
}
class Factory
{
public function create($type) {
switch($type) {
case 1:
$object = new A1($a, $b, $c);
case 2:
$object = new A2($a, $d, $e);
case N:
$object = new A1($a);
}
}
}
Answer the question
In order to leave comments, you need to log in
I would use a service locator to generate a list of services and pass it to the factory constructor.
Alternatively, you could define the factory as a service subscriber , in which case building the service locator would probably be even easier.
If in some way (for example, through a common interface or a static method of a factory class), you define a list of services that a factory can create, then you can create a compiler pass that will form a service locator at the container compilation stage by analyzing their dependencies. This is a little more complicated, but it won't require you to edit the code when expanding the list of classes generated by the factory.
Usually, REgistry
FooInterface
Service1Foo
Service2Foo
Service3Foo FooRegistry is
done through tags or through call, specific services are registered, in the factory we get through type (if through call regalia with the right key) or in a cycle, banishing each dependency and calling supports($type) for each of the services
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question