Search code examples
zend-framework2servicemanager

Zend framework 2 get specific type services


How can I get every class that implements or extends from a specific class ? For example I want to get all of classes that instantiated from MyClassName class ?


Solution

  • You can do what Exlord said , but if you want something framework specific , you can add your preferd classes in config file under a specific key and get all of them .
    for example :

    Module A :

    return array(
        'MyClassName' => array(
            'Module\Service\ClassA' =>  'Course\Service\ClassA',
            'Module\Service\ClassB' =>  'Course\Service\ClassB',
      );
    

    Module B :

    return array(
        'MyClassName' => array(
            'Module\Service\ClassC' =>  'Course\Service\ClassC',
      );
    

    and get all of MyClassName instances like this :

    $config = $this->getServiceLocator()->get('config');
    $MyClassNameInstances = $config['MyClassName'];