As I've been reading through the documentation and examples, I've found that a LoggerInterface is passed through the constructor of a dependency, as seen here, and this allows dependencies to output to the console.
My questions are:
If I have multiple classes do I keep passing along a
LoggerInterfacein the constructor or is there a better practice for this? Or should I not even be passing it to these classes?If I want to make a class work with out a
LoggerInterface, would I always just check if$this->loggeris null and then act accordingly?
For example,
class ObjectOne {
/**
* @var LoggerInterface
*/
private $logger;
public function __construct (LoggerInterface $logger) {
$this->logger = $logger;
}
}
class ObjectTwo {
/**
* @var ObjectOne[]
*/
private $items;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct (LoggerInterface $logger) {
$this->logger = $logger;
}
public function addObject () {
$this->items[] = new ObjectOne($this->logger);
}
}