简体   繁体   English

我是否以正确的方式使用 Auraphp 进行依赖注入?

[英]Am I using Auraphp in the right way for dependency injection?

I started playing with Auraphp for dependency injection, and I wrote a sample application.我开始使用 Auraphp 进行依赖注入,并编写了一个示例应用程序。 It is working as expected, however, I am not sure if I use it in the correct way.它按预期工作,但是,我不确定我是否以正确的方式使用它。 Can someone let me know if I am doing right, or is there any better way to use Aura?有人可以让我知道我是否做得对,或者有没有更好的方法来使用 Aura?

This is my public/index.php:这是我的 public/index.php:

use Aura\Di\ContainerBuilder;
use MyPackage\Base\Service;
use MyPackage\Base\Flow;

require_once dirname(__DIR__) . '/vendor/autoload.php';

$builder = new ContainerBuilder();
$di = $builder->newInstance();

$di->set('baseService', new Service);
$di->set('baseFlow', new Flow);

$service = $di->get('baseService');
$flow = $di->get('baseFlow');

$service->showMessage();
$flow->showMessage();

This is src/Service.php (src/Flow.php is similar):这是 src/Service.php(src/Flow.php 类似):

<?php

namespace MyPackage\Base;

class Service
{
    public function showMessage()
    {
        echo "Inside service";
    }
}

I mainly want to know if I am benefiting from dependency injection advantages.我主要想知道我是否受益于依赖注入的优势。 Besides, using Aura this way is not memory/CPU/time overloading?另外,这样使用Aura是不是内存/CPU/时间超载?

Any thoughts would be appreciated.任何想法将不胜感激。

$di->set('baseService', new Service);
$di->set('baseFlow', new Flow);

In this case you are already instantiating the class.在这种情况下,您已经在实例化该类。 But in most cases you can use lazy loading.但在大多数情况下,您可以使用延迟加载。 In that approach you will also benefit from inserting necessary dependencies needed.在这种方法中,您还将受益于插入所需的必要依赖项。 So your code will become所以你的代码会变成

$di->set('baseService', $di->lazyNew('Service'));
$di->set('baseFlow', $di->lazyNew('Flow'));

The example is taken from : http://auraphp.com/packages/3.x/Di/services.html .该示例取自: http : //auraphp.com/packages/3.x/Di/services.html

Assume your Flow class have some dependencies, you can either do by constructor injection or setter injection .假设您的 Flow 类有一些依赖项,您可以通过构造函数注入setter 注入来完成


class Flow
{
    public function __construct(Service $service)
    {
         $this->service = $service;
    }

    public function setSomething(Something $something)
    {
        // other dependency
    }

    public function showMessage()
    {
        echo "Inside service";
    }
}

Now you can define like现在你可以定义像

$di->params['Flow']['service'] = $di->lazyNew('Service');
$di->setters['Flow']['setSomething'] = $di->lazyNew('Something');

You can see more examples in the documentation .您可以在文档中查看更多示例 DI sometimes feels magic, but if you understand it correctly it will help you debug things quickly. DI 有时感觉很神奇,但如果您正确理解它,它将帮助您快速调试。

I noticed someone dislike the answer.我注意到有人不喜欢这个答案。 You are free to do it, but provide a better answer or edit the answer or write comments what can be improved.您可以自由地这样做,但提供更好的答案或编辑答案或写评论可以改进的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM