Created
November 9, 2012 05:51
-
-
Save Rican7/4043958 to your computer and use it in GitHub Desktop.
PHP 5.3+ Singleton Pattern Class (with aliases for ease of use)
This file contains 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 | |
// PHP 5.3+ Singleton Pattern | |
abstract class Singleton { | |
// Constructor | |
private function __construct() { | |
// Do nothing | |
} | |
// Don't allow cloning | |
final private function __clone() { | |
// Do nothing | |
} | |
// Get the instance of the class | |
final public static function instance() { | |
// Create an instance var to keep track of the instance | |
static $instance = null; | |
// If the instance doesn't exist yet | |
if ( is_null( $instance ) ) { | |
// Create it and keep a reference to it | |
$instance = new static(); | |
} | |
// Always return the instance | |
return $instance; | |
} | |
// Alias the instance method | |
final public static function getInstance() { | |
return static::instance(); | |
} | |
// Alias the instance method | |
final public static function get_instance() { | |
return static::instance(); | |
} | |
} // End class Singleton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment