-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DX] Null Object pattern for 'LoggerInterface $logger' #15594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This idea was proposed in a PR, but rejected for performance reasons. (see #14682) |
@dosten I don't think that performance degrades too much by using this pattern. I've added a comment in the related PR to ask Symfony deciders to reconsider it. |
I would recommend the following syntax: $this->logger = $logger ?: new NullLogger(); |
I don't think this should be labelled DX at all: DX does not apply to the internal implementation, only to the interface we provide. |
As it does not bring any "value", I'm closing this ticket. |
using null object is good solution... strange last comment from fabpot |
I noticed that most of the time in Symfony, when a LoggerInterface is injected into a service and when it can be null, we do not use the null-object pattern.
Psr\Log has a NullLogger that can be used in this case. It says :
https://github.com/php-fig/log/blob/master/Psr/Log/NullLogger.php
If found 60 occurences of "if (null !== $this->logger)" in Symfony. Many of these could be avoided and the code could be clearer.
Before:
After:
If you think it's a good idea, i can provide a PR.
The text was updated successfully, but these errors were encountered: