BookingController
BookingRepository
Logger
BookingController
BookingRepository
Logger
<?php
class BookingController {
/** @var RepositoryInterface */
private $repository;
/** @var LoggerInterface */
private $logger;
public function __construct() {
$this->repository = new BookingRepository();
$this->logger = new Logger();
}
}
BookingController
BookingRepository
Logger
<?php
class BookingController {
/** @var RepositoryInterface */
private $repository;
/** @var LoggerInterface */
private $logger;
public function __construct() {
$this->repository = BookingRepository::getInstance();
$this->logger = Logger::getInstance();
}
}
BookingController
BookingRepository
Logger
Notwendige Abhängigkeit
Hinreichende Abhängigkeit
Notwendige Abhängigkeit
Hinreichende Abhängigkeit
<?php
class BookingController {
/** @var RepositoryInterface */
private $repository;
/** @var LoggerInterface */
private $logger;
public function __construct(RepositoryInterface $repository) {
$this->repository = $repository;
$this->logger = new NullLogger(); // NullObject Pattern
}
public function setLogger(LoggerInterface $logger) {
$this->logger = $logger;
}
}
<?php // from "psr/log" within "monolog/monolog".
namespace Psr\Log;
/**
* This Logger can be used to avoid conditional log calls
*
* Logging should always be optional, and if no logger is provided to your
* library creating a NullLogger instance to have something to throw logs at
* is a good way to avoid littering your code with `if ($this->logger) { }`
* blocks.
*/
class NullLogger extends AbstractLogger
{
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return null
*/
public function log($level, $message, array $context = array())
{
// noop
}
}
DI Container
Gib mir 'controller.booking'
controller.booking
repository.booking
logger
entity_manager