1.8. Singleton

    To have only one instance of this object in the application that willhandle all calls.

    • DB Connector
    • Logger (may also be a Multiton if there are many log files forseveral purposes)
    • Lock file for the application (there is only one in the filesystem…)

    You can also find this code on GitHub

    Tests/SingletonTest.php

    1.  
    2. namespace DesignPatterns\Creational\Singleton\Tests;
    3.  
    4. use DesignPatterns\Creational\Singleton\Singleton;
    5. use PHPUnit\Framework\TestCase;
    6.  
    7. class SingletonTest extends TestCase
    8. {
    9. {
    10. $firstCall = Singleton::getInstance();
    11. $secondCall = Singleton::getInstance();
    12.  
    13. $this->assertInstanceOf(Singleton::class, $firstCall);
    14. $this->assertSame($firstCall, $secondCall);
    15. }
    16. }