Purpose:

This is considered to be an ANTI-PATTERN! We could use dependency injection to replace this pattern.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

To have only one instance of object in the application that will handle all calls.

 Creational --- Singleton 随笔

<?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
    }
}

  

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄