ServiceManager basically acts as a container. Inside the container, you satisfy the various dependencies of the created object and then return it for use by other objects.
So, somehow SM sits above the object, and does not enter the object. If you use an SM instance inside an object (possibly to access other services), you go against the principle of inverse control.
Below are two ways:
class A { private $data; public function __constructor($sm) { $this->data = $sm->get('user_data');
Another way
class B { private $data; public function __constructor($user_data) { $this->data = $user_data;
Somewhere inside Module.php :
'factories'=> array( 'objB'=> function($sm) {
In the second example, the dependency ( $user_data ) is injected into the object.
source share