Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Forked from amacgregor/UserSingleton.php
Created April 23, 2018 10:21
Show Gist options
  • Save gemmadlou/7d736e9cb1c641b0561af2ac6926c9fb to your computer and use it in GitHub Desktop.
Save gemmadlou/7d736e9cb1c641b0561af2ac6926c9fb to your computer and use it in GitHub Desktop.
PHP Singleton pattern example
<?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