add a simple example with a null logger

This commit is contained in:
Trismegiste
2013-05-13 21:49:33 +02:00
parent 3fcd8fbcba
commit ceeab58eb9
4 changed files with 104 additions and 0 deletions

30
NullObject/Service.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\NullObject;
/**
* Service is dummy service that uses a logger
*/
class Service
{
protected $logger;
// we inject the logger in ctor and it is mandatory
public function __construct(LoggerInterface $log)
{
$this->logger = $log;
}
public function doSomething()
{
// no more check "if (!is_null($this->logger))..." with the NullObject pattern
$this->logger->log('We are in ' . __METHOD__);
// something to do...
}
}