PHP Singleton Pattern Example
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 | |
class Singleton | |
{ | |
protected static $instance = null; | |
protected function __construct() | |
{ | |
# Thou shalt not construct that which is unconstructable! | |
} | |
protected function __clone() | |
{ | |
# Me not like clones! Me smash clones! | |
} | |
public static function getInstance() | |
{ | |
if (!isset(static::$instance)) { | |
echo "Creating Instance \n"; | |
static::$instance = new static; | |
} | |
echo "Returning instance \n"; | |
return static::$instance; | |
} | |
} | |
Singleton::getInstance(); | |
Singleton::getInstance(); | |
Singleton::getInstance(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment