mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-29 01:49:51 +02:00
58 lines
1.0 KiB
PHP
58 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace DesignPatterns;
|
|
|
|
/**
|
|
* Singleton pattern
|
|
*
|
|
* Purpose:
|
|
* to have only one instance of this object in the application that will handle all calls
|
|
*
|
|
* Examples:
|
|
* - DB Connector
|
|
* - Logger (may also be a Multiton if there are many log files for several purposes)
|
|
* - Lock file for the application (there is only one in the filesystem ...)
|
|
*
|
|
*/
|
|
class Singleton
|
|
{
|
|
/**
|
|
* gets the instance via lazy initialization (created on first usage)
|
|
*
|
|
* @return Singleton
|
|
*/
|
|
public static function getInstance()
|
|
{
|
|
static $instance;
|
|
|
|
if (null === $instance) {
|
|
$instance = new self();
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
* is not allowed to call from outside: private!
|
|
*
|
|
*/
|
|
private function __construct()
|
|
{}
|
|
|
|
/**
|
|
* prevent the instance from being cloned
|
|
*
|
|
* @return void
|
|
*/
|
|
private function __clone()
|
|
{}
|
|
|
|
/**
|
|
* prevent from being unserialized
|
|
*
|
|
* @return void
|
|
*/
|
|
private function __wakeup()
|
|
{}
|
|
}
|