Skip to content
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

Update README.md #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,25 @@ If you need a logger, you can use the interface like this:
```php
<?php

declare(strict_types=1);

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class Foo
final readonly class Foo
{
private $logger;
public function __construct(
private LoggerInterface $logger = new NullLogger()
){}

public function __construct(LoggerInterface $logger = null)
public function doSomething(): void
{
$this->logger = $logger;
}
$this->logger->info('Doing work');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to do modern PHP, IMO it's best to leave $logger nullable and call it conditionally with:

Suggested change
$this->logger->info('Doing work');
$this->logger?->info('Doing work');

That avoids useless method calls and a useless NullLogger instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends.

That means:

  • having your code riddled with "?->"
  • depending on a static code analysis tool (like PHPStan or Psalm) to prevent nullpointer exceptions

I think this is more a question of deprecating "NullLogger" or not (which is out of scope for this PR).

Personally, I like to keep it simple and stick to OOP with DI, since it is language agnostic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this package should deprecate NullLogger. Both NullLogger and the nullsafe operator are valid ways to solve the case of the optional logger in the constructor

Personally, I like to keep it simple and stick to OOP with DI, since it is language agnostic.

being language agnostic does not make sense when writing a code snippet for PHP.

And if you mean avoiding features that don't directly translate to other OOP languages, your argument is void IMO:

  • you are using constructor promoted properties, which have a direct equivalent in very few other languages
  • the nullsafe operator exists in most modern languages

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a recommended way to handle with no logs - pass a null , not NullLogger?


public function doSomething()
{
if ($this->logger) {
$this->logger->info('Doing work');
}

try {
$this->doSomethingElse();
} catch (Exception $exception) {
$this->logger->error('Oh no!', array('exception' => $exception));
} catch (Throwable $e) {
$this->logger->error('Oh no!', ['exception' => $e]);
}

// do something useful
Expand Down