-
-
Save gemmadlou/7d736e9cb1c641b0561af2ac6926c9fb to your computer and use it in GitHub Desktop.
PHP Singleton pattern example
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 | |
/** Example taken from http://www.webgeekly.com/tutorials/php/how-to-create-a-singleton-class-in-php/ **/ | |
class User | |
{ | |
// Hold an instance of the class | |
private static $instance; | |
// The singleton method | |
public static function singleton() | |
{ | |
if (!isset(self::$instance)) { | |
self::$instance = new __CLASS__; | |
} | |
return self::$instance; | |
} | |
} | |
$user1 = User::singleton(); | |
$user2 = User::singleton(); | |
$user3 = User::singleton(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment