title: ZF2 - Call to a member function on a non-object error
body: <p>I am trying to implement a simple roll-a-dice service and use it in a <code>.phtml</code> file. I know my issue has been reported often on SO, but I could not find a solution in other questions.</p>
  
  <p>I get the following error message:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function getDiceResult()
          on a non-object in rolladice.phtml on line 11</p>
  </blockquote>
  
  <p>Here is line 11:</p>
  
  <pre><code>&lt;?php
      $result =  $this-&gt;rollADiceService-&gt;getDiceResult();
      echo "&lt;p&gt;Roll-a-dice result: ".$result."&lt;/p&gt;";
  ?&gt;
  </code></pre>
  
  <p>The controller is set-up as following:</p>
  
  <pre><code>class RollADiceController extends AbstractActionController
  {
  
      private $rollADiceService;
  
      public function setPluginManager(PluginManager $plugins) {
          parent::setPluginManager($plugins);
          $this-&gt;rollADiceService = $this-&gt;getServiceLocator()
              -&gt;get('RollADiceService');
      }
      ...
  </code></pre>
  
  <p>In <code>Module.php</code>, I have:</p>
  
  <pre><code>public function getServiceConfig()
  {
      return array(
          'factories'=&gt;array(
              'LoginLogoutService' =&gt; function() {
                  return new LoginLogoutService();
              },
              'RollADiceService' =&gt; function() {
                  return new RollADiceService();
              },
          ),
      );
  }
  </code></pre>
  
  <p>I am using the same technique (i.e., <code>setPluginManager</code>) to retrieve instances of my services in other controllers without issues. What am I doing wrong?</p>
  
  <p><strong>P.S.:</strong> Using a debugger, I can see that <code>setPluginManager()</code> is called, but that the <code>$this-&gt;rollADiceService</code> variable is initialized with <code>null</code>. Why?</p>