Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two good options to get factories from service managers. One is the creation of factory classes, which happens most time in the Zend Framework code itself. The second one is using closures, as you are doing.</p> <p>Make sure you do not type things like:</p> <pre><code>'test-model' =&gt; Dashboard\Model\TestModel(), </code></pre> <p>But a real closure like your first one is a good example. Secondly, the Service Manager always gives an exception when you try to get a service which fails to instantiate. Note this exception does not include the message why: the class might not be found or an exception is thrown during instantiation (for example because the service manager cannot instantiate a dependency of the service you are trying to get).</p> <p>A last remark is you do not need to import FQCN (fully qualified class names) with <code>use</code> statements at the location you are trying to <em>get</em>. But you need to import the FQCNs when you are trying to <em>instantiate</em>.</p> <p>So this works:</p> <pre><code>&lt;?php class MyClass { protected $sm; public function setServiceManager($sm) { $this-&gt;sm = $sm; } public function doSomething() { $this-&gt;sm-&gt;get('some-special-key'); } } </code></pre> <p>And this too:</p> <pre><code>&lt;?php use Foo\Bar\Baz; $serviceConfig = array( 'factories' =&gt; array( 'some-special-key' =&gt; function($sm) { return new Baz; } ), ); </code></pre> <p>But this not (if you try to get a <code>Foo\Bar\Baz</code>):</p> <pre><code>&lt;?php $serviceConfig = array( 'factories' =&gt; array( 'some-special-key' =&gt; function($sm) { return new Baz; } ), ); </code></pre> <p>You might want to checkout my <a href="https://github.com/juriansluiman/SlmCmfKernel" rel="nofollow">SlmCmfKernel</a> repository. In my <a href="https://github.com/juriansluiman/SlmCmfKernel/blob/master/Module.php#L30" rel="nofollow">Module.php</a> I include a service configuration file, which is put in a <a href="https://github.com/juriansluiman/SlmCmfKernel/blob/master/config/services.config.php" rel="nofollow">separate location</a>. In <a href="https://github.com/juriansluiman/SlmCmfKernel/blob/master/Module.php#L48" rel="nofollow">another part of the code</a> I get a service from the manager.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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