专栏首页PHP-咪啪咪啪PHP-DI中文文档(基于有道翻译,基本是直接拿过来使用,并没有润色)

PHP-DI中文文档(基于有道翻译,基本是直接拿过来使用,并没有润色)

原文地址(http://php-di.org/doc/getting-start

Getting started with PHP-DI

(开始使用PHP-DI)

Welcome! This guide will help you get started with using PHP-DI in your project.

Before beginning, you need to know what dependency injection is. If you don't, there's a whole article dedicated to it: Understanding dependency injection.

(欢迎光临!本指南将帮助您在项目中开始使用PHP-DI。

在开始之前,您需要知道依赖注入是什么。如果你还不了解它的含义,这里有一整篇文章专门介绍它:理解依赖注入)

Installation

(安装) Install PHP-DI with Composer: (使用composer安装PHP-DI)

composer require php-di/php-di

PHP-DI requires PHP 7.0 or above. (PHP-DI需要PHP 7.0或者更高)

Basic usage

(基本用法)

1. Use dependency injection

(使用依赖注入) First, let's write code using dependency injection without thinking about PHP-DI: (首先,让我们在不考虑PHP-DI的情况下使用依赖注入编写代码:)

class Mailer
{
    public function mail($recipient, $content)
    {
        // send an email to the recipient
    }
}
class UserManager
{
    private $mailer;

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

    public function register($email, $password)
    {
        // The user just registered, we create his account
        // ...

        // We send him an email to say hello!
        $this->mailer->mail($email, 'Hello and welcome!');
    }
}

As we can see, the UserManager takes the Mailer as a constructor parameter: this is dependency injection! (就像我们所看到的这样,“UserManager”将“Mailer”作为构造函数参数:这就是依赖项注入!)

2. Create the container

(创建容器) You can create a container instance pre-configured for development very easily: (您可以很容易地创建一个预先配置的容器实例:)

$container = new Container();

If you want to register definition files (explained in PHP definitions) or tweak some options, you can use the container builder: (如果您想注册定义文件(在PHP definitions中解释)或调整一些选项,您可以使用容器构建器:)

$builder = new DI\ContainerBuilder();
$builder->...
$container = $builder->build();

3. Create the objects

(创建对象) Without PHP-DI, we would have to "wire" the dependencies manually like this: (如果没有PHP-DI,我们将不得不像这样手动地“连接”依赖项:)

$mailer = new Mailer();
$userManager = new UserManager($mailer);

Instead, we can let PHP-DI figure out the dependencies: (相反,我们可以让PHP-DI计算出依赖项:)

$userManager = $container->get('UserManager');

Behind the scenes, PHP-DI will create both a Mailer object and a UserManager object. (在幕后,PHP-DI将创建一个Mailer对象和一个UserManager对象。)

How does it know what to inject? (它怎么知道我们要注入什么对象?)

The container uses a technique called autowiring. This is not unique to PHP-DI, but this is still awesome. It will scan the code and see what are the parameters needed in the constructors.

In our example, the UserManager constructor takes a Mailer object: PHP-DI knows that it needs to create one. Pretty basic, but very efficient. (该容器使用一种称为autowiring自动连接的技术。 这并不是PHP-DI特有的,但这仍然是很棒的。 它将扫描代码并查看构造函数中需要的参数。

在我们的示例中,UserManager构造函数接受一个Mailer对象:PHP-DI知道它需要创建一个。 很基本,但很有效。)

Wait, isn't that weird and risky to scan PHP code like that? (等等,扫描PHP代码那不是很奇怪而且有风险的吗?)

Don't worry, PHP-DI uses PHP's Reflection classes which is pretty standard: Laravel, Zend Framework and many other containers do the same. Performance wise, such information is read once and then cached, it has no impact. (不要担心,PHP-DI使用了PHP的反射类 ,这是相当标准的:Laravel、Zend Framework和许多其他容器都是这样做的。性能方面,这些信息被读取一次,然后就会缓存起来,它没有任何影响。)

Defining injections

(定义注入) We have seen autowiring, which is when PHP-DI figures out automatically the dependencies a class needs. But we have 3 ways to define what to inject in a class: (我们已经看到了autowiring自动链接,即PHP-DI自动计算出类需要的依赖关系。但是我们有3种方法来定义在一个类中注入什么:)

return [
    'api.url'    => 'http://api.example.com',
    'Webservice' => function (Container $c) {
        return new Webservice($c->get('api.url'));
    },
    'Controller' => DI\create()
        ->constructor(DI\get('Webservice')),
];

Please read the Defining injections documentation to learn about autowiring, annotations and PHP definitions. (请阅读Defining injections的注入文档,了解autowiring, annotations and PHP definitions。)

Framework integration

(集成到框架中) We have seen in the example above that we can use the container to get objects: (我们在上面的例子中已经看到,我们可以使用容器来获取对象:)

$userManager = $container->get('UserManager');

However we don't want to call the container everywhere in our application: it would couple our code to the container. This is known as the service locator antipattern - or dependency fetching rather than injection. (但是,我们不希望在应用程序中到处调用容器:它会将我们的代码与容器耦合。这被称为服务定位器反模式或依赖抓取而不是注入。) To quote the Symfony documentation: (引用Symfony文档:)

You will need to get [an object] from the container at some point but this should be as few times as possible at the entry point to your application. 您需要从容器中获取(一个对象),但这应该是在您的应用程序的入口点上尽可能少的时间。

For this reason, PHP-DI integrates with some frameworks so that you don't have to call the container (dependencies are injected in controllers): (出于这个原因,PHP-DI集成了一些框架,这样您就不必调用容器(依赖项被注入控制器):)

If you want to use PHP-DI with another framework or your own code, try to use $container->get() in you root application class or front controller. Have a look at this demo application built around PHP-DI for a practical example. (如果您希望使用另一个框架或您自己的代码使用PHP-DI,请尝试在您的根应用程序类或前端控制器中使用$container->get()。我们来看看这个围绕PHP-DI构建的演示应用程序。)

What's next

(接下来是什么) You can head over to the documentation index. You can also read the Best practices guide, it's a good way to get a good view on when to use each of PHP-DI's features. 你可以去查阅文档索引。您还可以阅读最佳实践指南,这是了解何时使用PHP-DI特性的好方法。 下面是一些你现在可能感兴趣的话题: Here are some other topics that might interest you right now: (下面是一些你现在可能感兴趣的话题:)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 推荐20-PHP 7.4的FFI支持可以很好地利用PHP中的C函数/结构

    即将在下个月底发布的PHP 7.4中一个有趣的功能之一是期待已久的FFI(外部函数接口)支持。PHP 7.4的FFI允许开发人员可以从本地PHP代码中调用C编程...

    猿哥
  • CVE-2019-11043: PHP 7 RCE漏洞分析

    研究人员在PHP 7中找出有个远程代码执行(RCE)漏洞,该漏洞CVE编号为CVE-2019-11043。攻击者利用该漏洞只需要访问通过精心伪造的URL就可以在...

    猿哥
  • 如何理解 RESTful 的幂等性

    HTTP幂等方法,是指无论调用多少次都不会有不同结果的 HTTP 方法。不管你调用一次,还是调用一百次,一千次,结果都是相同的。

    芋道源码
  • 【译】现代化的PHP开发--TDD

    来源/https://www.startutorial.com/articles/view/modern-php-developer-tdd 译/Lemon黄...

    Lemon黄
  • 某套颜色 CMS 的几处后台 Getshell

    在某资源站上发现一个专门搞颜色的 CMS (说啥专注 X 站),于是下载下来审计一波

    信安之路
  • 图解从 URL 到网页通信原理

    互联网的原始目的,就是为了传输文本(文本对话)。那我们使用浏览器发送请求后页面是如何呈现在我们面前的呢? 接下来由图片介绍下URL到呈现页面的过程。

    芋道源码
  • PHP的stdClass

    在百度上搜索,有很多人写的文章中,提到了stdClass是所有类的基类,但是我试了一下:

    烟草的香味
  • 大型网站架构演进的五大阶段盘点

    一个创业公司起步时很可能就两台机器,一台Web 服务器、一台数据库服务器,在一个应用系统中集成了所有功能模块,但随着业务的发展、流量的增长,单应用远远不能满足业...

    芋道源码
  • RESTful 架构基础

    REST(Representational State Transfer)架构风格是一种世界观,把信息提升为架构中的一等公民。通过 REST 可以实现系统的高性...

    芋道源码
  • day80-<权限组件>-基本流程以及部分代码

    天下第六

扫码关注云+社区

领取腾讯云代金券