I have a class called classB which has multiple dependencies which I am setting up using ClassBFactory. Inside classA I would like to create a new instance of classB using the classBFactory.
Is the only way to get a new instance of classB from its factory by injecting the whole service container into classA, or is there another way? It would seem bad practice to pass in the whole service container as this would give the class access to a lot more than needed.
I've experimented with the following but this won't work as createService() requires the service container, which I would rather not inject into classA
class ClassA {
protected $classBFactory;
public function __construct(ClassBFactory $classBFactory)
{
$this->classBFactory = $classBFactory;
}
public function getNewA()
{
return $this->classBFactory->createService();
}
}