# Loading modules not working

**URL:** <https://discourse.laminas.dev/t/loading-modules-not-working/3024>\
**Category:** Migration\
**Created:** [August 25, 2022, 2:00pm UTC](https://discourse.laminas.dev/t/loading-modules-not-working/3024 "2022-08-25T14:00:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ushiro](https://avatars.discourse-cdn.com/v4/letter/u/ed8c4c/32.png) [@ushiro](https://discourse.laminas.dev/u/ushiro)\
**Post date:** [August 25, 2022, 2:00pm UTC](https://discourse.laminas.dev/t/loading-modules-not-working/3024/1 "2022-08-25T14:00:00Z")

</div>

I’ve been struggling with this issue for 2 days now, so any help would be really appreciated. We are trying to migrate our laminas-mvc project to 3.x. There is this piece of code which loads the modules for php-unit tests which I have not been able to get it to work.

Previous (worked fine):

```auto
 public static function getServiceManager(array $config = null)
    {
        $config = $config ?: static::getApplicationConfig();
        $serviceManager = new ServiceManager(
            new ServiceManagerConfig(
                isset($config['service_manager']) ? $config['service_manager'] : []
            )
        );
        $serviceManager->setService('ApplicationConfig', $config);

        /* @var $moduleManager \Laminas\ModuleManager\ModuleManagerInterface */
        $moduleManager = $serviceManager->get('ModuleManager');

        $moduleManager->loadModules();

        return $serviceManager;
    }

```

And this is the adapted piece of code which throws `Call to a member function attach() on null`:

```auto
public static function getServiceManager(array $config = null)
    {
        $config = $config ?: static::getApplicationConfig();
        $serviceManager = new ServiceManager(isset($config['service_manager']) ? $config['service_manager'] : []);
        $serviceManager->setService('ApplicationConfig', $config);
        $listenerOptions = new ListenerOptions(isset($config['module_listener_options']) ? $config['module_listener_options'] : []);
        $defaultListeners = new DefaultListenerAggregate($listenerOptions);

        $moduleManager = new ModuleManager(isset($config['modules']) ? $config['modules'] : []);
        $defaultListeners->attach($moduleManager->getEventManager());

        $moduleManager->loadModules();

        return $serviceManager;
    }

```

Any hint provided would be much appreciated, thank you in advance.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [August 25, 2022, 2:17pm UTC](https://discourse.laminas.dev/t/loading-modules-not-working/3024/2 "2022-08-25T14:17:15Z")

</div>

Hello and welcome to our forums! 😃

Do you use [laminas-test](https://docs.laminas.dev/laminas-test/)? If so, then none of this is needed.
