如何使用“依赖注入”在简单的php功能,我应该打扰吗? [英] How can I use "Dependency Injection" in simple php functions, and should I bother?

查看:116
本文介绍了如何使用“依赖注入”在简单的php功能,我应该打扰吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说人们一直在谈论依赖注入和它的好处,但我不太明白。

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 ).

我读了这个依赖注入文章( 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();

任何人都有一个很好的资源或只是洞察力,使方法和利益 - 晶体清除? p>

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".

这是当你不喜欢全局变量时,在awfull 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);

现在,诀窍是使用单个类来管理依赖项,类似于: p>

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功能,我应该打扰吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆