PHP依赖注入 [英] PHP Dependency Injection

查看:136
本文介绍了PHP依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是,由于某种原因,如果由于某种原因,我的类依赖于几个类,而不是将所有这些类传递给构造函数中的这一类,是否有更好的,更明智的方法?



我听说过DI容器,这是怎么解决这个问题的?我应该从哪里开始这个解决方案?我是否将依赖关系传递给我的DIC,然后将其传递给需要这些依赖关系的类?



任何可以指向正确方向的帮助将是非常棒的。

解决方案

如果您有几个依赖关系来处理,那么是一个DI容器就可以解决了。



DI容器可以是由您需要的各种依赖对象,它被传递给构造函数并解包。



假设您需要一个配置对象,数据库连接和传递给每个类的客户端信息对象。您可以创建一个包含它们的数组:

  //假设每个都是单独创建或访问的,但需要... 
//这可以在脚本的顶部全局创建,并传入每个新的
//实例化的类
$ di_container = array(
'config'= new Config (),
'db'= new DB($ user,$ pass,$ db,$ whatever),
'client'= new ClientInfo($ clientid)
);

您的类构造函数接受DI容器作为参数:

  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'];
}
}

而不是像上面的那样(就是简单),您还可以将DI容器创建为类本身,并将其分别注入到组件类中实例化。使用对象而不是数组的一个好处是,默认情况下,它将通过引用传递给使用它的类,而数组通过值传递(尽管数组中的对象仍然是引用)。



编辑



尽管对代码最初的代码更复杂,但有一些对象比数组更灵活的方法。 >

容器对象也可以在其构造函数中创建/实例化所包含的类(而不是在外部创建它们并将其传递)。这可以为每个使用它的脚本节省一些编码,因为您只需要实例化一个对象(本身就是实例化其他对象)。

  Class DIContainer {
public $ config;
public $ db;
public $ client;

// DI容器可以构建自己的成员对象
public function __construct($ params ....){
$ this-> config = new Config() ;

//这些变量可以在构造函数中传递,也可以是常量或其他东西
$ this-> db = new DB($ user,$ pass,$ db, $等等);

//与此相同 - var可能来自构造函数$ _SESSION或其他地方
$ this-> client = new ClientInfo($ clientid);
}
}


I'm trying to get my head around Dependency Injection and I understand it, for the most part.

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?

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?

Any help that would point me in the right direction would be fantastic.

解决方案

If you have several dependencies to deal with, then yes a DI container can be the solution.

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.

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

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'];
  }
}

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

Edit

There are some ways in which an object is more flexible than an array, although more complicated to code initially.

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);
  }
}

这篇关于PHP依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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