mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-06 15:06:31 +02:00
cs
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\NullObject;
|
||||
|
||||
/**
|
||||
* LoggerInterface is a contract for logging something
|
||||
*
|
||||
* Key-feaature : NullLogger MUST inherit from this interface like any other Loggers
|
||||
* Key feature: NullLogger MUST inherit from this interface like any other Loggers
|
||||
*/
|
||||
interface LoggerInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function log($str);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\NullObject;
|
||||
|
||||
/**
|
||||
@@ -32,10 +28,11 @@ namespace DesignPatterns\NullObject;
|
||||
*/
|
||||
class NullLogger implements LoggerInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
*/
|
||||
public function log($str)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\NullObject;
|
||||
|
||||
/**
|
||||
@@ -11,10 +7,11 @@ namespace DesignPatterns\NullObject;
|
||||
*/
|
||||
class PrintLogger implements LoggerInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
*/
|
||||
public function log($str)
|
||||
{
|
||||
echo $str;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\NullObject;
|
||||
|
||||
/**
|
||||
@@ -11,20 +7,28 @@ namespace DesignPatterns\NullObject;
|
||||
*/
|
||||
class Service
|
||||
{
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
// we inject the logger in ctor and it is mandatory
|
||||
/**
|
||||
* we inject the logger in ctor and it is mandatory
|
||||
*
|
||||
* @param LoggerInterface $log
|
||||
*/
|
||||
public function __construct(LoggerInterface $log)
|
||||
{
|
||||
$this->logger = $log;
|
||||
}
|
||||
|
||||
/**
|
||||
* do something ...
|
||||
*/
|
||||
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...
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user