php di,PHP-DI中文文档(基于有道翻译,基本是直接拿过来使用,并没有润色)
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種方法來定義在一個類中注入什么:)
使用 PHP definitions
Every one of them is different and optional. Here is an example of PHP definitions in a file:
(每一個都是不同的和可選的。下面是一個文件中的PHP定義示例:)
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:
(下面是一些你現在可能感興趣的話題:)
總結
以上是生活随笔為你收集整理的php di,PHP-DI中文文档(基于有道翻译,基本是直接拿过来使用,并没有润色)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java表示新年快乐,2017新年快乐:
- 下一篇: 动态规划算法php,php算法学习之动态