I have a logger class I made and I’m not sure which is the best way to implement it.
The write() method in this class needs to be able to be called from any method from within every class in the application.
Previously I was doing Logger::write() havin the method as a static method. It’s quick and easy but I have a feeling it’s not the best way to do it.
Should I inject an instance of the logger class into my other objects (via constructor or setter method) and then when I need to write to a log call it like:
$this->logger->write()
Why do you want it to be available within every object? Injecting it everywhere seems very tedious and is not very practical. What if you need to log something inside a static method, a regular function or in the global scope?
Or to put it simply: What is wrong with just calling
Logger::write()? Maybe you are over-thinking it?