PHP Null Object Design Pattern

From wikipedia: In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (“null”) behavior. This pattern is used to deal with the absence of an object by providing an alternative that indicates this absence. In short the null object replaces the checking for null values. These classes also adhere to the Open-Close Principle.

This pattern changes:

if (!is_null($obj)) { $obj->callMethod(); }
to just 
$obj->callMethod();

– Client code is simplified
– Reduces the chance of null pointer exceptions
– Fewer conditionals require less test cases


execute();
    }
}

$outputCommand = new OutputCommand();
$fileCommand = new FileCommand();
$app = new Application();

// Echo predefined string
$application->run($outputCommand); // Output from the command

// Create a file and append string to it
$application->run($fileCommand);

// Do nothing
$application->run();