Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

php - 在PHP的DI容器中使用别名时,不能使用自动连线吗?(Can't I use auto wires when using aliases in DI containers in PHP?)

class A
{
    public function __construct(B $b)
    {
        $this->b = $b;
    }
}

class B
{
    public function __construct(C $c)
    {
        $this->c = $c;
        $c->say();
    }
}

class C
{
    public function say()
    {
        echo 'This is C';
    }
}

class D
{
    public function say()
    {
        echo 'This is D';
    }
}

//Dice DI container
$container = new DiceDice;
$instance = $container->create(A::class); //This is C

$container->addRules([
    '*' => [
        'substitutions' => [C::class => D::class],
    ],
]);
$instance = $container->create(A::class); //This is C  // But I expect 'D'

//League DI container
$container = new LeagueContainerContainer;
$container->delegate(new LeagueContainerReflectionContainer);
$instance = $container->get(A::class);
$container->add(C::class, D::class);
$instance = $container->get(A::class); //Fatal error  :(

I used aliases to inject interfaces into classes in League Container.

(我使用别名将接口注入到League Container中的类中。)

This is good here.

(这很好。)

However, using dependency injection in the injected class gave me an error.

(但是,在注入的类中使用依赖注入会给我一个错误。)

After a lot of trial and error, it seemed to be a problem with League Container, so I created a DI container interface to replace the DI container, fixed the code accordingly, and changed it to Dice.

(经过大量的试验和错误,这似乎是League Container的问题,因此我创建了一个DI容器接口来替换DI容器,相应地修复了代码,并将其更改为Dice。)

I was prepared to try all the containers.

(我准备尝试所有的容器。)

No ..... I tried to make my work simple before using another container.

(不.....在尝试使用另一个容器之前,我试图简化我的工作。)

I knew I was misunderstanding something.

(我知道我误会了一些东西。)

Two days of work are driving me crazy....

(两天的工作使我发疯....)


OK... Self-answer.

(OK ...自我解答。)

Aura DI and PHP DI failed.

(Aura DI和PHP DI失败。)

Maybe I'm in a hurry and don't know how to use it.

(也许我很着急,不知道如何使用它。)

Or their usage is somewhat complicated for beginners.

(否则它们的使用对于初学者来说有些复杂。)

Use Zend DI.

(使用Zend DI。)

It works fine regardless of the old version.

(无论使用哪个旧版本,它都可以正常工作。)

Even this

(即使这样)

class A
{
    public function __construct(B $b)
    {
        $this->b = $b;
    }
}

class B
{
    public function __construct(E $e)
    {
    }
}

class C implements E
{
    public function __construct(D $d)
    {
        $d->say();
    }
    public function say()
    {
        echo 'This is C.<br>';
    }
}

class D implements E
{
    public function say()
    {
        echo 'This is D.<br>';
    }
}

interface E
{

}

//Zend-di 3.1.1
use ZendDiInjector;
use ZendDiConfig;

$injector = new Injector(new Config([
    'preferences' => [
        E::class => C::class,
    ],
]));
$instance = $injector->create(A::class); //This is D


//Zend-di 2.6.1
$di = new ZendDiDi();
$im = $di->instanceManager();
$im->addAlias(E::class, C::class);
$instance = $di->get(A::class); //This is D
  ask by ??? translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...