Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have some problems understanding why service factory should not work on your case. Do you need to return different service class unrelated to each other?</p> <p>What I see from the factory example is that you could do something like this:</p> <pre><code>class NewsletterFactory { public function __constructor(...) { // Receive arguments needed to create the service below } public function get() { // Say the variable $userInput exists and is derived from constructor if ($userInput === 'string') return new NewsletterManager($dynamicArgument1); if ($userInput === 'integer') return new AnotherNewsletterManager($dynamicArgument2); return new DefaultNewsletterManager(); } } </code></pre> <p>Now, if this doesn't fit your needs. You can also create a service say <code>CustomFactory</code> that returns what you need. What is returned is not directly a service, so you can do whatever you want. But this would prevent you from requesting the objects created by <code>CustomFactory</code> from the dependency container.</p> <p>Similar to that is the <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/FormFactory.php" rel="nofollow">FormFactory</a>. It is the factory used to instantiate form type. But the <code>FormFactory</code> is more powerfull since it is coupled with a dependency injection tag and a compiler pass, which register each types into the dependency injection system so they can be retrieved on their own. I don't exactly all the internals of the <code>Form</code> component but I think it could solve your problem if other methods do not.</p> <p>Regards,<br/> Matt</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload