- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据您的样本Github您可以在构造函数中注入(inject)记录器接口(interface),默认值为 NULL。
<?php
use Psr\Log\LoggerInterface;
class Foo
{
private $logger;
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}
public function doSomething()
{
if ($this->logger) {
$this->logger->info('Doing work');
}
// do something useful
}
}
表达某个东西有一个 Logger,您可以实现 Psr\Log\LoggerAwareInterface
和 Psr\Log\LoggerAwareTrait
。
重建示例代码,它看起来像这样
<?php
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
class Foo implements LoggerAwareInterface
{
use LoggerAwareTrait;
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}
public function doSomething()
{
if ($this->logger) {
$this->logger->info('Doing work');
}
// do something useful
}
}
那很好并且可以工作,但是如果我愿意这样做
<?php
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
class Foo implements LoggerAwareInterface
{
use LoggerAwareTrait;
public function __construct(LoggerInterface $logger = null)
{
$this->setLogger( $logger );
}
public function doSomething()
{
if ($this->logger) {
$this->logger->info('Doing work');
}
// do something useful
}
}
最终会出现必须是 Psr\Log\LoggerInterface 的实例,给定 null
错误,因为接口(interface)中的方法声明没有 NULL 默认值。当然,可以通过使用 if 进行保护或传递 NullLogger 来防止此错误,但这很奇怪。
能够在构造函数中传递可选的 Logger 实例将使我认为稍后可以通过将 Logger 设置为 NULL 值来更改该实例。当然,这是示例代码,但让我们看看问题
public function __construct(LoggerInterface $logger = null);
public function setLogger(LoggerInterface $logger);
所以基本上我可以将 NULL 引用传递到构造函数中,但我无法调用 setter,因为不允许 NULL。如果 Psr\Log\LoggerAwareInterface
看起来像这样,那就更好了
<?php
namespace Psr\Log;
/**
* Describes a logger-aware instance.
*/
interface LoggerAwareInterface
{
/**
* Sets a logger instance on the object.
*
* @param LoggerInterface $logger
*
* @return void
*/
public function setLogger(LoggerInterface $logger = null);
}
那么请告诉我这个决定的背景?
最佳答案
我认为您在这里混淆了很多问题。
示例用法https://github.com/php-fig/log#usage展示如何在应用程序中使用 psr/log
实现。它也做得正确。
所以下一个问题是关于 LoggerAwareInterface 的用法和通过 LoggerAwareTrait 的 setLogger
方法
public function __construct(LoggerInterface $logger = null)
{
$this->setLogger($logger);
}
如果您的构造函数接受 null,则不应调用 setLogger
方法。 setLogger
方法只能接受 LoggerInterface
并且它不会意外地需要将记录器对象本身设置为 null。
假设签名为setLogger($logger = null)
。现在,如果您调用 setLogger()
(如下例所示),您可以看到记录器将重置为 null。
$logger = new SomePSR-3Logger();
$foo = new Foo($logger);
$foo->setLogger();
如果您想实现 PSR-3 记录器,您应该考虑阅读:https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
希望有帮助。
谢谢。
关于php - PSR 日志 - 为什么 LoggerAwareInterface 和 LoggerAwareTrait 没有 NULL 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40401259/
TD;LR:我试图掌握中间件实现背后的理念。它似乎可以正常工作,但我该如何正确处理响应,以便它向浏览器显示基本授权登录提示? -- 我正在使用: equip/dispatch用于 PSR-15 中间件
我将我的一些 bundle 的 psr-0 自动加载更改为 psr-4。现在,当我尝试使用 app/console 命令生成实体时,它给了我 Can't find base path for "MbD
最近我读到了关于命名空间以及它们的好处的文章。我目前正在 Laravel 中创建一个项目,并尝试从类映射自动加载转移到命名空间。但是,我似乎无法理解 PSR-0 和 PSR-4 之间的实际区别。 我读
我开始研究 Composer 并正在开发一个系统,我将文件核心应用程序文件分开,如下所示: /root |-- /src |-- /App |-
我有一个在内部项目中使用的 composer 包。历史上,此包中的所有类都是通过 "autoload": { "classmap": ... } 自动加载的,并且尚未结构化。 现在我想迁移到 PSR-
我正在尝试 PHP 编码标准包:https://github.com/inpsyde/php-coding-standards ,但是当我开始运行 phpcs 时,我得到: nickan@nickan
我正在尝试 PHP 编码标准包:https://github.com/inpsyde/php-coding-standards ,但是当我开始运行 phpcs 时,我得到: nickan@nickan
我尝试使用 PSR-0 自动加载文件,但它不会自动加载该文件。我用 PSR-4 自动加载尝试了相同的文件。使用 PSR-4,它可以完美运行。 PSR-0 所需的文件夹结构有什么不同吗? 即使保留 Wh
我正在使用 Laravel 5.0,一切正常。 但是,我不断收到这些警告消息。我已经查看了 JetBrains 文档,但我仍然有点不清楚该怎么办。 本质上,我担心如果我允许 PhpStorm 自动运行
我是 C++ 的新手,想了解一些东西。我的 main.cpp 中有这段代码: Radio r = Radio("PSR", 100.8); 或者那个代码: Radio r("PSR", 100.8);
我是 C++ 的新手,想了解一些东西。我的 main.cpp 中有这段代码: Radio r = Radio("PSR", 100.8); 或者那个代码: Radio r("PSR", 100.8);
我知道您可以使用 PSR 标准来定位文件,或者告诉 composer 一个目录来扫描类。 documentation recommends using the PSR-4标准。 Composer 还可
Composer 是否有来自 CLI 的任何命令添加到 composer.json 这样的入口? { "autoload": { "psr-4": {
根据 PSR-2 Visibility MUST be declared on all properties and methods 但是 __construct 呢,它是特定的功能,我想知道当我们使
PSR建议,方法名称必须在camelCase中声明,而类名称必须在StudlyCaps中声明。 最佳答案 StudlyCaps,也称为PascalCase,表示每个子单词的首字母大写。 camelCa
任何人都可以建议 PSR 标准文档中的哪个位置记录了类成员变量中的变量类型后面不应有任何内容吗? 我用了PHP CodeStyle Fixer通过执行以下操作: php-cs-fixer 修复 MyC
我希望我的 IDE (PhpStorm) 对所有自己的文件强制执行 PSR-2 兼容格式。然而,有时我必须提交遗留代码的补丁,我只想提交功能更改,而不是代码样式更改。 我如何使用 PhpStorm 仅
我有一个在本地运行良好的 Laravel 项目(Mavericks),但是 psr-4 下的类没有加载到我们的舞台服务器(CentOS)上。每次尝试 composer update 或运行 artis
我创建了公司供应商库,但 namespace 有问题。 通常我会忽略这一点,但现在想要解决问题。 我现在测试示例供应商 Symfony\Bundle\FrameworkBundle但这有同样的错误。
我想从 lib 加载 HomeController 类目录: root/ -lib/ --/HomeController.php -vendor/ -composer.json -index.php
我是一名优秀的程序员,十分优秀!