PHP design pattern code examples of null object (25)



The empty object pattern is not a GOF design pattern, but it is enough to be considered a design pattern as a frequent pattern. It has the following advantages:

  • The client code is simple

  • It can reduce the probability of null pointer exception

  • Test cases don’t need to consider too many conditions

Return an object or null should be replaced by a return object or nullobject. Nullobject simplifies rigid code and eliminates condition checking in client code, such as if (! Is)_ Null ($obj)) {$obj – > callsomething();} just $obj – > callsomething().


  • Symfony2: empty log

  • Symfony2: empty output of symfony / console

  • Empty command in command line mode

  • Empty processor in chain of responsibility mode

UML diagram

The official PHP advanced learning exchange community “click” manages and sorts out some materials. Bat and other first-line factories have advanced knowledge systems (related learning materials and written test questions) and are not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, swote, soft, Kafka, MySQL optimization, shell script, dock Er, micro service, nginx and other knowledge points


  • Service.php
logger = $logger;

     *What to do...
     *'we are in service: dosomething' is returned in the diary.
    public function doSomething()
        //Tip: Here you just use it, not through_ Null () checks if $logger is set.
        $this->logger->log('We are in '.__METHOD__);
  • LoggerInterface.php
  • PrintLogger.php
  • NullLogger.php


  • Tests/LoggerTest.php

     *Test the printlogger object. If you contact the above, you can know that designpatterns / behavioral / nullobject / service:: dosomething has been written in the journal.
    public function testStandardLogger()
        $service = new Service(new PrintLogger());
        $this->expectOutputString('We are in DesignPatterns\Behavioral\NullObject\Service::doSomething');

PHP Internet architect’s way to grow * “design patterns” Ultimate Guide

PHP Internet architect 50K growth Guide + general outline of industry problem solving (continuously updated)

Interview 10 companies, get 9 offers, PHP interview questions in 2020

If you like my article and want to communicate and learn with more senior developers, you can get more technical advice and guidance related to interview with large factories. Welcome to join our group, code: phpzh (junyang number: 856460874).

The latest PHP advanced tutorial 2020, full series!

If the content is good, I hope you can support and encourage you to click like / like, and you are welcome to exchange views. In addition, if you have any questions, you can suggest the content you want to see in the comments

Recommended Today

JS multi image upload, get a number of images Base64 value display to the page

Display effect before uploadingDisplay effect after uploading $(‘#img_purchase’).on(‘click’,function(){ $(‘#purchase’).click(); }); $(‘#img_sale’).on(‘click’,function(){ $(‘#sale’).click(); }); function getBase64 (file,Callback) { let reader = new FileReader() let imgResult = ” reader.readAsDataURL(file) reader.onload = function () { imgResult = reader.result } reader.onerror = function (error) { Callback(error) } reader.onloadend = function () { Callback(imgResult) } }; $(document).on(‘change’, ‘#purchase’, function () […]