ServiceManager는 기본적으로 컨테이너 역할을합니다. 컨테이너 내부에서 작성한 객체의 다양한 종속성을 충족시킨 다음 다른 객체가 사용하도록 반환합니다.
그래서 SM이 개체 위에 앉는 방식으로 개체 내부로 들어 가지 않습니다. 객체 내부에서 SM 인스턴스를 사용하고 있다면 (아마 다른 서비스에 액세스하는 경우) Inversion of Control이라는 원칙을 위반하게됩니다. 두 번째 예 의존성 ($user_data
)으로
'factories'=>
array(
'objB'=> function($sm) {
//this is the container where sm sites outside the object to satisfy its dependencies
$objB = new B($sm->get('user_data'));
return $objB;
}
)
개체에 주입 얻는다 :
다음
는
class A {
private $data;
public function __constructor($sm) {
$this->data = $sm->get('user_data'); // Service manager accessed inside the object
}
}
다른 방법 Module.php
내부 어딘가에
class B {
private $data;
public function __constructor($user_data) {
$this->data = $user_data; //$user_data getting injected from sm container
}
}
은 두 가지이다.
정확히 어디서 얻으시겠습니까? – Crisp