title: ZF2 Service Manager: How to get a namespace class instance?
body: <p>Building on my previous question: <a href="https://stackoverflow.com/questions/24982221/zf2-autoloader-use-factory-for-base-class-on-extended-classes-as-well">ZF2 Autoloader: Use factory for base class on extended classes as well</a></p>
  
  <p>I'm trying to get an instance of a subclass of a class I've attached an abstract factory for in my service manager config. I know the factory is being called, because I've got an echo in it that is working. The problem I seem to be running into though is that I don't know how to ask the service manager for my class and get it properly.</p>
  
  <p>When I make this call:</p>
  
  <pre><code>$x = $serviceManager-&gt;get('some\class\baseClass');
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>Error: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an 
         instance for some\class\name
  </code></pre>
  
  <p>And when I output the name that is being passed into the factory, it is missing the slashes like so:</p>
  
  <pre><code>someclassbaseclass
  </code></pre>
  
  <p>Class File</p>
  
  <pre><code>use Zend\ServiceManager\AbstractFactoryInterface;
  use Zend\ServiceManager\ServiceLocatorInterface;
  
  class baseClassFactory implements \Zend\ServiceManager\AbstractFactoryInterface
  {
      public function canCreateServiceWithName (ServiceLocatorInterface $locator, $name, $requestedName = '')
      {
          return ('baseClass' === $name || is_subclass_of($name, 'baseClass'));
      }
  
      public function createServiceWithName (ServiceLocatorInterface $locator, $name, $requestedName = '')
      {
          $db = $locator-&gt;get('db');
          $query = new $name($db);
          return $query;
      }
  }
  </code></pre>
  
  <p>Configuration</p>
  
  <pre><code>$serviceManager =&gt; array(
      'abstract_factories' =&gt; array(
          'baseClassFactory',
      ),
  );
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>After testing this code, I've worked out all the bugs. This is the code behind a correct factory accounting for namespaces:</p>
  
  <pre class="lang-php prettyprint-override"><code>namespace some\class;
  
  use Zend\ServiceManager\AbstractFactoryInterface;
  use Zend\ServiceManager\ServiceLocatorInterface;
  
  class baseClassFactory implements \Zend\ServiceManager\AbstractFactoryInterface
  {
      public function canCreateServiceWithName (ServiceLocatorInterface $locator, $name, $requestedName = '')
      {
          $baseClassName = 'some\class\baseClass';
          return ($baseClassName === $requestedName || is_subclass_of($requestedName, $baseClassName));
      }
  
      public function createServiceWithName (ServiceLocatorInterface $locator, $name, $requestedName = '')
      {
          // This class and sub-classes need a database connection
          $db = $locator-&gt;get('db');
  
          // Use the fully qualified path to the class so it inits correctly
          $createName = '\\' . $requestedName;
  
          // Get the system class instance
          $class = new $createName($db);
  
          // Pass it back to the user
          return $class;
      }
  }
  </code></pre>