Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6876025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:25:36+00:00 2026-05-27T04:25:36+00:00

Imagine I have a bunch of classes, that can do logging, so I’ve created

  • 0

Imagine I have a bunch of classes, that can do logging, so I’ve created a logger interface and a couple of implementations (writing to file, to stdout, to db, etc.), but sometimes I don’t care about logging those messages, so I’ve made an implementation, that simply ignores all of the messages.

Now this helps to avoid ifs and just use $this->logger->write($message), but I still have to inject the dummy logger every time.

So would it do any harm to do stuff like: $this->logger = $logger ? $logger : new DummyLogger() in the constructor.

I generally do no work in the constructor, but this kind of stuff doesn’t seem too dangerous.

Would you choose this approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:25:36+00:00Added an answer on May 27, 2026 at 4:25 am

    Before answering your question, I’d suggest to rethink your Logger architecture. Logging is always the same. You write a message to somewhere. The only thing that differs is the somewhere part, so it makes sense to separate your Logger into a generic Logger and various Writers. That allows for greater flexibility, for instance, you can use the Composite pattern to write to multiple Loggers at once.

    Regarding your question: in general you want to avoid hardcoding dependencies into your code because it will directly impact on testability and reusability, e.g. your Logger can only be used with this particular NullWriter then. If you are going to distribute the Logger you will also have to distribute the Writer as well.

    However, assuming that you would only distribute the Logger with the entire package of Writers anyway and you also provide the means to do ctor injection, I dont see much of an issue. You can still swap out the Writer if needed, so all is well.

    It’s somewhat different when we are talking about interpackage dependencies. You will want to avoid these, e.g. your classes in the Database package should not depend on classes in the Logger package.

    An alternative to assigning the NullWriter from inside the Logger would be to use a LoggerFactory that creates the Logger and a specified Writer, injects the Writer and returns the Logger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

imagine I have a bunch of C++ related classes (all extending the same base
Imagine that you have a column with a bunch of fields like this: ab
Imagine we have a program trying to write to a particular file, but failing.
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
Imagine I have a process that starts several child processes. The parent needs to
I have a bunch of 'rowviews' that I want to put in a vertical
Say you have a bunch of files. Say you can store meta data to
Imagine I have a folder, inside it are a bunch of photos from my
Imagine I have the following 'partial' which is generated through a bunch of views:
I have a batch invite command where users can enter a bunch of emails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.