PHP中两种类型的依赖注入之间的区别

huangapple go评论73阅读模式
英文:

PHP the difference between 2 type of dependency injection

问题

我想请你对事情提出意见。

2种不同的注入方法之间的差异是什么?
我向你解释一下代码:

第一种方式是将要在制造商中使用的类注入:

/**
 * @var FileUploader
 */
private FileUploader $fileUploader;

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

第二种方式是直接注入到函数中:

/**
 * @Route("/", name="admin_images_index", methods={"GET"})
 * @param ImagesRepository $imagesRepository
 * @return Response
 */
public function index(ImagesRepository $imagesRepository): Response
{
    $query = $imagesRepository->findByImage();

    return $this->render('images/index.html.twig', [
        'images' => $query,
    ]);
}

所以我的问题是:

这两种方法之间有什么区别?应该选择哪一种?哪一种效率最高?还是只是两种不同的风格?

如果有人知道我的问题的答案,那将非常好,

非常感谢您未来的答复,祝大家好运。

英文:

I’d like to ask you for an opinion on things

What is the deficiency between 2 addictive injection method?
I explain to you the code:

the first is to inject the Class to be used in the manufacturer:


/**
     * @var FileUploader
     */
    private FileUploader $fileUploader;
 
    public function __construct(FileUploader $fileUploader)
    {
        $this->fileUploader = $fileUploader;
    }

the second way is to inject directly into the function:

/**
     * @Route("/", name="admin_images_index", methods={"GET"})
     * @param ImagesRepository $imagesRepository
     * @return Response
     */
    public function index(ImagesRepository $imagesRepository): Response
    {
        $query = $imagesRepository->findByImage();
 
        return $this->render('images/index.html.twig', [
            'images' => $query,
        ]);
    }

So my question is this:

What is the difference between this 2 ways of doing things? Which one should one choose? Which one should be the most efficient? Or is it just 2 styles to do?

If anyone who knows the answer to my question could answer that would be really nice,

thank you very much for your future answer and good luck to all.

答案1

得分: 1

不同之处在于你的类的结构。如果你想在多个函数中使用注入的对象,你必须首先使用第一种方式以保持代码整洁。如果你只想在一个函数中使用,你必须使用第二种方式,因为你必须考虑负载和该类将要执行的操作。

更新

"负载" 的意思是:

当你将数据注入构造函数时,它将把所有数据放入内存中等待使用(以加快处理速度),所以让我们想一下,如果你的注入是一个非常大的对象(大量位数放入内存并且加载速度较慢),那么你的脚本将会响应更长时间,对于你编写的任何代码都是如此。如果你要在一个类中多次使用,例如一个大对象,那么你可以将它注入到构造函数中,无论如何,如果使用得当,代码就不会毫无意义。

总结:

如果只使用一次或者只是小部分,区别不大。但如果你的代码很重,那么负载会变得很慢。

使你的代码轻量化并且方便自己和计算机使用。

英文:

Difference is the structure of your class. If you want to use more then in one function the injecting object you must use first for pretty code. If you want to use only in one function you must use second, because you must to think about load and what will do that class.

Updated

The "load" mean:

Then you make injection into the constructor, it will put all data into ram and waiting for use it (for faster processing), so let's thing if that your injection is very big object (large bits amount placed into ram and slow load it), so your script will make respond longer, for everything what you code process. Then you will use more then in one class that for ex. large object, then you can inject it to constructor, anyway you will not pointless code if it will be use right.

Summary:

There is not very difference, if it only once using or small parts. But if you make have all your's code heavy, so your load will be same slowly.

Make your code light and easy to use your self and computer.

huangapple
  • 本文由 发表于 2020年1月6日 21:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定