問題描述
我正在努力了解依賴注入,并且在很大程度上我理解它.
I'm trying to get my head around Dependency Injection and I understand it, for the most part.
但是,假設(shè)由于某種原因,我的一個(gè)類依賴于多個(gè)類,而不是在構(gòu)造函數(shù)中將所有這些都傳遞給這個(gè)類,是否有更好、更明智的方法?
However, say if, for some reason, one of my classes was dependent on several classes, instead of passing all of these to this one class in the constructor, is there a better, more sensible method?
我聽說過 DI Containers,這是我解決這個(gè)問題的方式嗎?我應(yīng)該從哪里開始這個(gè)解決方案?我是否將依賴項(xiàng)傳遞給我的 DIC,然后將其傳遞給需要這些依賴項(xiàng)的類?
I've heard about DI Containers, is this how I would go about solving this problem? Where should I start with this solution? Do I pass the dependencies to my DIC, and then pass this to the class that needs these dependencies?
任何能幫我指明正確方向的幫助都會(huì)很棒.
Any help that would point me in the right direction would be fantastic.
推薦答案
如果您有多個(gè)依賴項(xiàng)需要處理,那么 DI 容器可以成為解決方案.
If you have several dependencies to deal with, then yes a DI container can be the solution.
DI 容器可以是由您需要的各種依賴對(duì)象構(gòu)成的對(duì)象或數(shù)組,這些對(duì)象會(huì)被傳遞給構(gòu)造函數(shù)并解包.
The DI container can be an object or array constructed of the various dependent object you need, which gets passed to the constructor and unpacked.
假設(shè)您需要一個(gè)配置對(duì)象、一個(gè)數(shù)據(jù)庫連接和一個(gè)傳遞給每個(gè)類的客戶端信息對(duì)象.您可以創(chuàng)建一個(gè)包含它們的數(shù)組:
Suppose you needed a config object, a database connection, and a client info object passed to each of your classes. You can create an array which holds them:
// Assume each is created or accessed as a singleton, however needed...
// This may be created globally at the top of your script, and passed into each newly
// instantiated class
$di_container = array(
'config' = new Config(),
'db' = new DB($user, $pass, $db, $whatever),
'client' = new ClientInfo($clientid)
);
并且您的類構(gòu)造函數(shù)接受 DI 容器作為參數(shù):
And your class constructors accept the DI container as a parameter:
class SomeClass {
private $config;
private $db;
private $client;
public function __construct(&$di_container) {
$this->config = $di_container['config'];
$this->db = $di_container['db'];
$this->client = $di_container['client'];
}
}
代替我上面所做的數(shù)組(這很簡單),您還可以將 DI 容器創(chuàng)建為一個(gè)類本身,并使用單獨(dú)注入其中的組件類對(duì)其進(jìn)行實(shí)例化.使用對(duì)象而不是數(shù)組的一個(gè)好處是,默認(rèn)情況下它將通過引用傳遞給使用它的類,而數(shù)組是通過值傳遞的(盡管數(shù)組中的對(duì)象仍然是引用).
Instead of an array as I did above (which is simple), you might also create the DI container as an class itself and instantiate it with the component classes injected into it individually. One benefit to using an object instead of an array is that by default it will be passed by reference into the classes using it, while an array is passed by value (though objects inside the array are still references).
對(duì)象在某些方面比數(shù)組更靈活,盡管最初的編碼更復(fù)雜.
There are some ways in which an object is more flexible than an array, although more complicated to code initially.
容器對(duì)象也可以在其構(gòu)造函數(shù)中創(chuàng)建/實(shí)例化包含的類(而不是在外部創(chuàng)建它們并將它們傳入).這可以為使用它的每個(gè)腳本節(jié)省一些編碼,因?yàn)槟恍枰獙?shí)例化一個(gè)對(duì)象(它本身實(shí)例化其他幾個(gè)).
The container object may also create/instantiate the contained classes in its constructor as well (rather than creating them outside and passing them in). This can save you some coding on each script that uses it, as you only need to instantiate one object (which itself instantiates several others).
Class DIContainer {
public $config;
public $db;
public $client;
// The DI container can build its own member objects
public function __construct($params....) {
$this->config = new Config();
// These vars might be passed in the constructor, or could be constants, or something else
$this->db = new DB($user, $pass, $db, $whatever);
// Same here - the var may come from the constructor, $_SESSION, or somewhere else
$this->client = new ClientInfo($clientid);
}
}
這篇關(guān)于PHP 依賴注入的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!