ZF2 instantiating classes from factories using MutableCreationOptionsTrait: creates the same instance of class no matter what

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2 instantiating classes from factories using MutableCreationOptionsTrait: creates the same instance of class no matter what

DimaD
This post has NOT been accepted by the mailing list yet.
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?
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 instantiating classes from factories using MutableCreationOptionsTrait: creates the same instance of class no matter what

Marco Pivetta
This post has NOT been accepted by the mailing list yet.
The service must be marked as "not shared" in order to make this work. By default, services are shared, so they won't be re-created over multiple requests of the same service name.


On 10 April 2016 at 10:50, DimaD [via Zend Framework Community] <[hidden email]> wrote:
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?


To start a new topic under Zend Framework, email [hidden email]
To unsubscribe from Zend Framework Community, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: ZF2 instantiating classes from factories using MutableCreationOptionsTrait: creates the same instance of class no matter what

DimaD
This post has NOT been accepted by the mailing list yet.
Thanks very much, Marco!