i will try to explain my question via an example:
we have 1 main php script, that instantiates a few classes/objects, our project specific code. We want to add a logger to all those classes. what is the common-practice way to do that? for the sake of an example, we want to have Monolog, its supposed to be instantiated with:
$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
$log->addWarning('Foo');
option #1: we could have 1 monolog per class. i guess not the best option.
option #2: from the main script, have a $log object, and pass it to each called object (and then in each object __construct assign the $log to a private variable $class_logger and then use it:
for example, main script:
$logger = Logger::getLogger("main");
$logger->info("This is an informational message.");
$logger->warn("I'm not feeling so good...");
$test_run = new called_object($logger);
$test_run->call_it();
called_object class:
class called_object {
private $logger;
public function __construct($log)
{
$this->logger = $log;
}
public function call_it()
{
$this->logger->warn("from called_object!");
}
}
second seems better, but still i feel its not too practical, perhaps i am missing something here?