Created
April 26, 2016 02:20
-
-
Save zmiftah/0cd3a7c96066929b67fa7d8eb6e7fe25 to your computer and use it in GitHub Desktop.
PHP Design Pattern : Singleton Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Source : http://www.phptherightway.com/pages/Design-Patterns.html | |
*/ | |
class Singleton | |
{ | |
/** | |
* @var Singleton The reference to *Singleton* instance of this class | |
*/ | |
private static $instance; | |
/** | |
* Returns the *Singleton* instance of this class. | |
* | |
* @return Singleton The *Singleton* instance. | |
*/ | |
public static function getInstance() | |
{ | |
if (null === static::$instance) { | |
static::$instance = new static(); | |
} | |
return static::$instance; | |
} | |
/** | |
* Protected constructor to prevent creating a new instance of the | |
* *Singleton* via the `new` operator from outside of this class. | |
*/ | |
protected function __construct() | |
{ | |
} | |
/** | |
* Private clone method to prevent cloning of the instance of the | |
* *Singleton* instance. | |
* | |
* @return void | |
*/ | |
private function __clone() | |
{ | |
} | |
/** | |
* Private unserialize method to prevent unserializing of the *Singleton* | |
* instance. | |
* | |
* @return void | |
*/ | |
private function __wakeup() | |
{ | |
} | |
} | |
$obj = Singleton::getInstance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment