forked from Djuki/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNullLogger.php
41 lines (36 loc) · 1.26 KB
/
NullLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\NullObject;
/**
* NullOutput is a example of NullObject pattern. It is not formely a Design
* Pattern by the GoF but it's a schema which appears frequently enough to
* be a pattern. Futhermore it is a really good pattern in my opinion :
* - the code in the client is simple
* - it reduces the chance of null pointer exception
* - less "if" => less test cases
*
* The purpose : every time you have a method which returns an object or null,
* you should return an object or a "NullObject". With NullObject, you don't need
* statement like "if (!is_null($obj)) { $obj->callSomething(); }" anymore.
*
* In this case, this a logger which does nothing. Other examples :
* - null logger of symfony profiler
* - null output in symfony/console
* - null handler in a Chain of Responsiblities pattern
* - null command in a Command pattern
*
* Performance concerns : ok there is a call for nothing but we spare an "if is_null"
* I didn't run a benchmark but I think it's equivalent.
*
* Key feature : of course this logger MUST implement the same interface (or abstract)
* like the other loggers.
*/
class NullLogger implements LoggerInterface
{
public function log($str)
{
// do nothing
}
}