In ZF2, I have a factory like this
class SomeServiceFactory implements FactoryInterface, MutableCreationOptionsInterface
{
use MutableCreationOptionsTrait;
public function createService(ServiceLocatorInterface $serviceLocator)
{
$serviceManager = $serviceLocator->getServiceLocator();
$formElementManager = $serviceManager->get('FormElementManager');
if ($this->creationOptions == 'value1') {
return new SomeService(
$formElementManager->get('Path\To\Form1'),
$serviceManager->get('Path\To\Mapper1'),
new Object1()
);
} elseif ($this->creationOptions == 'value2') {
return new SomeService(
$formElementManager->get('Path\To\Form2'),
$serviceManager->get('Path\to\Mapper2'),
new Object2()
);
}
}
}
In the controller factory, I get several instances of SomeService based on the option value attached at the object creation, like
$service1 = $viewHelperManager->get('Path\To\SomeService', ['valueType' => 'value1']);
$service2 = $viewHelperManager->get('Path\To\SomeService', ['valueType' => 'value2']);
(these services are view helpers with their dependencies).
The problem is that $service2 is the exact same object as $service1 whereas it should have different dependencies. I tried to study the thing a bit, and it seems that the $creationOptions are not updated when assigning $service2 despite the valueType is completely different.
What is wrong?