Purpose:
This is considered to be an ANTI-PATTERN! We could use dependency injection to replace this pattern.
To have only one instance of object in the application that will handle all calls.
<?php
namespace DesignPatterns\Creational\Singleton;
final class Singleton
{
/**
* @var Singleton
*/
private static $instance;
/**
* Gets the instance via lazy initialization (created on first usage)
*
* @return Return Singleton
*/
public static function getInstance(): Singleton
{
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Private the constructor
*
* @return Void
*/
private function __construct()
{
// Code Here
}
/**
* Prevent the instnace from being cloned (which would create a second instance of it)
*
* @return Void
*/
private function __clone()
{
// Code Here
}
/**
* Prevent from being unserialized (which would create a second instance of it)
*
* @return Return
*/
private function __weakup()
{
// Code Here
}
}