English 中文(简体)
ZF2 依赖性注射
原标题:ZF2 Dependancy Injection Alias and multiple instances

We are currently writing a module in Zend Framework 2.

我在寻找这方面的文件时遇到了一些麻烦,但知道这样做是可能的。

我有3个等级,我想通过依赖注射来设置。 听说这些班级;A级、B级和C级。

班级A有一系列的B级,B级有一系列的C级。 每一类B应采用不同的参数进行即时处理。 对于C级,所有3个班级的参数均通过建筑商通过(如果需要,也可由一组人处理)。

<?php
class ClassA {
    protected $arrClassBInstances = array();

    public function __construct( $arrClassBInstances ) {
         $this->arrClassBInstances = $arrClassBInstances;
    }
}

class ClassB {
    protected $arrClassCInstances = array();
    protected $someOtherParam = "";

    public function __construct( $arrClassCInstances, $someOtherParam ) {
         $this->arrClassBInstances = $arrClassCInstances;
         $this->someOtherParam = $someOtherParam;

    }
}

class ClassC {
    protected $someParam = "";

    public function __construct( $someParam ) {
         $this->someParam = $someParam;
    }
}

So their are 2 parts to my questions. First, How can I configure multiple instances of the same class with different parameters injected? For Example, multiple instances of ClassB each with it s own subset of ClassC instances. There has to be some kind of alias in the ZF2 DiC.

第二,我怎么能够将一系列的重新组合的依附纳入一个类别。 例 我怎么能将一系列的B级例引入A级?

如果可能,请向我提供直接使用DiC的范例,以及在模块的DI部分(config.php)中实现这一榜样。

问题回答

From the documentation, i think this should works:

// $event instance of endMvcMvcEvent
$di = $event->getTarget()->getLocator();
$paramsForA = array(
    $di->get( qualified_namespaces_or_di_alias_for_b , array( arrClassCInstances =>array(
        $di->get( qualified_namespaces_or_di_alias_for_c , array( someParam =>1)),
        $di->get( qualified_namespaces_or_di_alias_for_c , array( someParam =>2)),
    ))),
    $di->get( qualified_namespaces_or_di_alias_for_b , array( arrClassCInstances =>array(
        $di->get( qualified_namespaces_or_di_alias_for_c , array( someParam =>3)),
        $di->get( qualified_namespaces_or_di_alias_for_c , array( someParam =>4)),
    ))),
);
$classA = $di->get( qualified_namespaces_or_di_alias_for_a , array( arrClassBInstances =>$paramsForA));

页: 1





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签