問題描述
我一直聽到人們談論依賴注入及其好處,但我并不真正理解.
I hear people talking about dependency injection and the benefit of it all the time, but I don't really understand it.
我想知道這是否是我一直將數據庫連接作為參數傳遞"問題的解決方案.
I'm wondering if it's a solution to the "I pass database connections as arguments all the time" problem.
我嘗試閱讀維基百科關于它的條目,但該示例是用 Java 編寫的,所以我不能完全理解它試圖闡明的區別.( http://en.wikipedia.org/wiki/Dependency_injection).
I tried reading wikipedia's entry on it, but the example is written in Java so I don't solidly understand the difference it is trying to make clear. ( http://en.wikipedia.org/wiki/Dependency_injection ).
我閱讀了這篇關于 php 依賴注入的文章 ( http://www.potstuck.com/2009/01/08/php-dependency-injection/ ),似乎目標不是直接將依賴項傳遞給對象,而是封鎖創建一個對象以及它的依賴項的創建.不過,我不確定如何在使用 php 函數的上下文中應用它.
I read this dependency-injection-in-php article ( http://www.potstuck.com/2009/01/08/php-dependency-injection/ ), and it seems like the objective is to not pass dependencies to an object directly, but to cordon off the creation of an object along with the creation of it's dependencies. I'm not sure how to apply that in a using php functions context, though.
另外,下面是依賴注入,我是否應該費心嘗試在函數上下文中進行依賴注入?
Additionally, is the following Dependency Injection, and should I bother trying to do dependency injection in a functional context?
版本 1:(我每天創建但不喜歡的那種代碼)
Version 1: (the kind of code that I create, but don't like, every day)
function get_data_from_database($database_connection){
$data = $database_connection->query('blah');
return $data;
}
版本 2:(不必傳遞數據庫連接,但也許不需要依賴注入?)
Version 2: (don't have to pass a database connection, but perhaps not dependency injection?)
function get_database_connection(){
static $db_connection;
if($db_connection){
return $db_connection;
} else {
// create db_connection
...
}
}
function get_data_from_database(){
$conn = get_database_connection();
$data = $conn->query('blah');
return $data;
}
$data = get_data_from_database();
版本3:(對象"/數據的創建是分開的,數據庫代碼是靜止的,所以這可能算作依賴注入?)
Version 3: (the creation of the "object"/data is separate, and the database code is still, so perhaps this would count as dependency injection?)
function factory_of_data_set(){
static $db_connection;
$data_set = null;
$db_connection = get_database_connection();
$data_set = $db_connection->query('blah');
return $data_set;
}
$data = factory_of_data_set();
有沒有人有很好的資源或只是洞察力,使方法和好處 - 晶瑩剔透?
Anyone have a good resource or just insight that makes the method and benefit -crystal- clear?
推薦答案
依賴注入是一個大詞,表示我的構造函數中有更多參數".
Dependency injection is a big word for "I have some more parameters in my constructor".
當你不喜歡全局變量時,這就是你在可怕的 Singleton 浪潮之前所做的:
It's what you did before the awfull Singleton wave when you did not like globals :
<?php
class User {
private $_db;
function __construct($db) {
$this->_db = $db;
}
}
$db = new Db();
$user = new User($db);
現在,訣竅是使用單個類來管理您的依賴項,就像這樣:
Now, the trick is to use a single class to manage your dependencies, something like that :
class DependencyContainer
{
private _instances = array();
private _params = array();
public function __construct($params)
{
$this->_params = $params;
}
public function getDb()
{
if (empty($this->_instances['db'])
|| !is_a($this->_instances['db'], 'PDO')
) {
$this->_instances['db'] = new PDO(
$this->_params['dsn'],
$this->_params['dbUser'],
$this->_params['dbPwd']
);
}
return $this->_instances['db'];
}
}
class User
{
private $_db;
public function __construct(DependencyContainer $di)
{
$this->_db = $di->getDb();
}
}
$dependencies = new DependencyContainer($someParams);
$user = new User($dependencies);
你一定認為你只是另一個類和更多的復雜性.但是,您的用戶類可能需要像許多其他類一樣記錄消息.只需將 getMessageHandler 函數添加到您的依賴項容器,并將一些 $this->_messages = $di->getMessageHandler()
添加到您的用戶類.其余代碼無需更改.
You must think you just another class and more complexity. But, your user class may need something to log messages like lot of other classes. Just add a getMessageHandler function to your dependency container, and some $this->_messages = $di->getMessageHandler()
to your user class. Nothing to change in the rest of your code.
你會得到很多關于 symfony 的文檔一個>
這篇關于我如何使用“依賴注入"?在簡單的 php 函數中,我應該打擾嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!