Note that there are some explanatory texts on larger screens.

plurals
  1. POService (uses service factory) to return objects of different types
    text
    copied!<p>I need to create a service, which uses another factory service, and return objects of different types.<br> Here's a definitions of my services:</p> <pre><code>services: games.search_factory: class: %games.search_factory.class% calls: - [ setContainer, ["@service_container"] ] games.search: class: %games.search.base.class% factory_service: games.search_factory factory_method: get </code></pre> <p>My <code>%games.search_factory.class%</code> <em>get</em> method returns different objects depending on the request parameters.<br> My <code>%games.search.base.class%</code> is abstract.<br> My goal is - when I call</p> <pre><code>$this-&gt;get("games.search"); </code></pre> <p>I want to get the result of factory method. But when I run the code, I get an error: <strong>FatalErrorException: Error: Cannot instantiate abstract class</strong>.</p> <p>So I have two questions: </p> <ol> <li>Why does DI system try to instantiate my class, instead of returning the result of my factory method?</li> <li>Is there a way to implement what I need (nicely and concisely)?</li> </ol> <p><strong>Update:</strong> I had a bug in the part of code, which is not shown here. Besides these two services for search, I had a service for each search type, and returned these services from a factory. But in the definition of these services, I've used my base class (which is abstract) as a class. </p> <p>So nice conclusion is that <strong><em>it is possible to return an objects of different types from a factory.</em></strong></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