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
- namespace DesignPatterns\Creational\Singleton\Tests;
- use DesignPatterns\Creational\Singleton\Singleton;
- use PHPUnit\Framework\TestCase;
- class SingletonTest extends TestCase
- {
- {
- $firstCall = Singleton::getInstance();
- $secondCall = Singleton::getInstance();
- $this->assertInstanceOf(Singleton::class, $firstCall);
- $this->assertSame($firstCall, $secondCall);
- }
- }