[英]Sharing the same instance of an object: auryn vs. PHP-DI
I am trying to build my first no-framework PHP application and I am following this tutorial .我正在尝试构建我的第一个无框架 PHP 应用程序,我正在关注本教程。 I am relatively new to some concepts described in the tutorial.
我对教程中描述的一些概念比较陌生。 Despite this, I decided to use, as Dependency Injector, PHP-DI instead of the suggested one ( rdlowrey/auryn ).
尽管如此,我还是决定使用PHP-DI作为依赖注入器,而不是建议的( rdlowrey/auryn )。
I have created everything according to the tutorial except for the file Bootstrap.php
(and the file Dependencies.php
:我已经根据教程创建了所有内容,除了文件
Bootstrap.php
(和文件Dependencies.php
:
<?php declare(strict_types = 1);
require(__DIR__ . '/../vendor/autoload.php');
...
$container = include('Dependencies.php');
$request = $container->make('Http\HttpRequest');
$response = $container->make('Http\HttpResponse');
...
switch ($routeInfo[0]) {
...
case \FastRoute\Dispatcher::FOUND:
$className = $routeInfo[1][0];
$method = $routeInfo[1][1];
$vars = $routeInfo[2];
$class = $container->make($className);
$class->$method($vars); // (**)
break;
}
echo $response->getContent(); // (*)
$class
can be only an instance of a Homepage
class which has only one method ( show()
), called in (**): $class
只能是Homepage
类的一个实例,它只有一个方法 ( show()
),在 (**) 中调用:
class Homepage
{
private $request;
private $response;
private $renderer;
public function __construct(
Request $request,
Response $response,
Renderer $renderer
) {
$this->request = $request;
$this->response = $response;
$this->renderer = $renderer;
}
public function show() {
$data = [
'name' => $this->request->getParameter('name', 'stranger'),
];
$html = $this->renderer->render('Homepage', $data);
$this->response->setContent($html); // (***)
}
}
With all that said, the application returns a 200 HTTP response with an empty body [here (*)] but if I try to print the content of the HTTP response after (***) I get the correct response.尽管如此,应用程序返回一个带有空正文的 200 HTTP 响应 [here (*)] 但是如果我尝试在 (***) 之后打印 HTTP 响应的内容,我会得到正确的响应。 This could mean that there are two different instances of an HttpResponse class.
这可能意味着 HttpResponse 类有两个不同的实例。 (Is that right?)
(那正确吗?)
By using rdlowrey/auryn, the author of the tutorial, used the method share()
to share the same HttpReponse instance among classes, as shown in the "original" Dependencies.php
file:通过使用 rdlowrey/auryn,教程的作者,使用方法
share()
在类之间共享相同的 HttpReponse 实例,如“原始” Dependencies.php
文件所示:
<?php declare(strict_types = 1);
use \Auryn\Injector;
...
$injector = new Injector;
$injector->alias('Http\Response', 'Http\HttpResponse');
$injector->share('Http\HttpResponse');
...
return $injector;
Is there a way to get the same behavior using PHP-DI (with PHP definitions)?有没有办法使用 PHP-DI(带有 PHP 定义)获得相同的行为?
Here's my version of Dependencies.php
:这是我的
Dependencies.php
版本:
<?php declare(strict_types = 1);
$definitions = [
'Http\Request' => DI\create('Http\HttpRequest')->constructor(
$_GET, $_POST, $_COOKIE, $_FILES, $_SERVER),
'Http\HttpRequest' => function () {
$r = new Http\HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
return $r;
},
'Http\Response' => DI\create('Http\HttpResponse'),
'Twig\Environment' => function () {
$loader = new Twig\Loader\FilesystemLoader(
dirname(__DIR__) . '/templates');
$twig = new Twig\Environment($loader);
return $twig;
},
'Example\Template\TwigRenderer' => function (Twig\Environment $renderer) {
return new Example\Template\TwigRenderer($renderer);
},
'Example\Template\Renderer' => DI\create(
'Example\Template\TwigRenderer')->constructor(
DI\get('Twig\Environment')),
];
$containerBuilder = new DI\ContainerBuilder;
$containerBuilder->addDefinitions($definitions);
$container = $containerBuilder->build();
return $container;
In Bootstrap.php
, getting ( get()
) HttpRequest
/ HttpResponse
instances, instead of making ( make()
) them, solved the problem.在
Bootstrap.php
,获取 ( get()
) HttpRequest
/ HttpResponse
实例,而不是制作 ( make()
) 它们,解决了问题。
...
$container = include('Dependencies.php');
$request = $container->get('Http\HttpRequest');
$response = $container->get('Http\HttpResponse');
...
As clearly stated in the documentation :正如文档中明确指出的:
The make() method works like get() except it will resolve the entry every time it is called.
make() 方法的工作方式与 get() 类似,只是它每次被调用时都会解析条目。 [..] if the entry is an object, an new instance will be created every time [..]
[..] 如果条目是一个对象,则每次 [..] 都会创建一个新实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.