# Nested classes depend on execution scope using ServiceManager

**URL:** <https://discourse.laminas.dev/t/nested-classes-depend-on-execution-scope-using-servicemanager/2906>\
**Category:** Components & MVC\
**Tags:** laminas-servicemanager\
**Created:** [June 2, 2022, 10:41am UTC](https://discourse.laminas.dev/t/nested-classes-depend-on-execution-scope-using-servicemanager/2906 "2022-06-02T10:41:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lon9man](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/lon9man/32/1314_2.png) [@lon9man](https://discourse.laminas.dev/u/lon9man)\
**Post date:** [June 2, 2022, 10:41am UTC](https://discourse.laminas.dev/t/nested-classes-depend-on-execution-scope-using-servicemanager/2906/1 "2022-06-02T10:41:33Z")

</div>

**PROBLEM**

1. we use ZF2 and have ServiceLocator inside many classes.
2. to migrate to ZF3/Laminas docs suggest to inject all dependencies inside class using Factory

**EXAMPLE**  
for ZF2, where we have ServiceLocator everywhere:

1. we have main `ClassA`, which have as parameter URL-string. we get its instance from Factory using ServiceLocator
2. on runtime `ClassA` make curl-request to URL and gets response
3. `ClassA` detects type of response (HTML, file, XML, JSON, …)
4. `ClassA` calls ServiceLocator (it calls Factory) to get appropriate `ClassX` dependently to type of response (`HTML_Adapter`, `File_Adapter`, `XML_Adapter`, `JSON_Adapter`, …, `100+ other adapters`), which will parse data using own logic and return the result

to simplify i **described only 2 levels of nesting** , but in reality it can be **deeper** and each level potentially can depend on execution scope.

if we are speaking about ZF3/Laminas then we should:  
a) remove ServiceLocator from everywhere  
b) build whole `ClassA` with all deep dependencies using Factory and inject it into controller before action will be run

as we can see **we unable** to build `ClassA`, because **we don’t know which concrete dependency** we will need as this information we will get after `ClassA` will start own execution.

or we should inject ALL 100+ adapters into `ClassA`, but in reality **we will need ONLY 1 of them** (it looks like performance issue). and also can exist other nuances, which will not allow to initiate all of them due to absented input data before execution of parent class starts.

**question:**

1. so what is the best practice in such case?
2. how to implement resolving nested classes in ZF2/ZF3/Laminas using Factory (and ServiceLocator) if each deeper level of nested classes depends on execution scope?

---

<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:** [June 3, 2022, 7:30am UTC](https://discourse.laminas.dev/t/nested-classes-depend-on-execution-scope-using-servicemanager/2906/2 "2022-06-03T07:30:42Z")

</div>

Hello and welcome to our forums! 😃

> [@lon9man](#):
>
> 1. we use ZF2 and have ServiceLocator inside many classes.

Correct this is a big problem and always been wrong, also in ZF2.

> [@lon9man](#):
>
> a) remove ServiceLocator from everywhere

Remove it from everywhere. Do not inject the entire service container / manager!

* * *

Check the concept of plugin managers:

> **[Plugin managers - laminas-servicemanager - Laminas Docs](https://docs.laminas.dev/laminas-servicemanager/plugin-managers/)**
>
> laminas-servicemanager: factory-driven dependency injection container

It is widely used in various components like:

- [https://github.com/laminas/laminas-inputfilter/blob/5d8986654c8b455192cd180985634f9eacf56501/src/InputFilterPluginManager.php#L18-L23](https://github.com/laminas/laminas-inputfilter/blob/5d8986654c8b455192cd180985634f9eacf56501/src/InputFilterPluginManager.php#L18-L23)
- [https://github.com/laminas/laminas-form/blob/beee25df568f1140d5553eabe51f97c6643f0464/src/FormElementManager.php#L22-L27](https://github.com/laminas/laminas-form/blob/beee25df568f1140d5553eabe51f97c6643f0464/src/FormElementManager.php#L22-L27)
- [https://github.com/laminas/laminas-mvc/blob/4beede6da28451ee962886adea9870a711552388/src/Controller/PluginManager.php#L10-L16](https://github.com/laminas/laminas-mvc/blob/4beede6da28451ee962886adea9870a711552388/src/Controller/PluginManager.php#L10-L16)
- [https://github.com/laminas/laminas-view/blob/3b5a1617128bb913d12fd10a29f3854ee563b2fc/src/HelperPluginManager.php#L26-L35](https://github.com/laminas/laminas-view/blob/3b5a1617128bb913d12fd10a29f3854ee563b2fc/src/HelperPluginManager.php#L26-L35)
- [https://github.com/laminas/laminas-validator/blob/bdd503adc83d814a5c94e598ea0eb9fc7ca56339/src/ValidatorPluginManager.php#L49](https://github.com/laminas/laminas-validator/blob/bdd503adc83d814a5c94e598ea0eb9fc7ca56339/src/ValidatorPluginManager.php#L49)

Or create separate container based on the [PSR-11 container interface](https://www.php-fig.org/psr/psr-11/) which handles only your adapters.
