Skip to content

Instantly share code, notes, and snippets.

@amacgregor
Last active March 8, 2024 07:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save amacgregor/8660951 to your computer and use it in GitHub Desktop.
Save amacgregor/8660951 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();
?>
@phpguru
Copy link

phpguru commented Apr 30, 2014

__CLASS__ should be self on line 14

@technokid
Copy link

technokid commented Apr 11, 2018

class User better use final class User and also add private function __construct(){} add private function __clone(){} and private function __wakeup(){}

@technokid
Copy link

technokid commented Apr 11, 2018

better example
`<?php
trait Singleton {
static private $instance = null;
private function __construct() { /* ... @return Singleton / } // Protect from creation through new Singleton
private function __clone() { /
... @return Singleton / } // Protect from creation through clone
private function __wakeup() { /
... @return Singleton / } // Protect from creation through unserialize
static public function getInstance() {
return
self::$instance===null
? self::$instance = new static()//new self()
: self::$instance;
}
}
/
*

  • Class Foo

  • @method static Foo getInstance()
    */
    class Foo {
    use Singleton;

    private $bar = 0;

    public function incBar() {
    $this->bar++;
    }

    public function getBar() {
    return $this->bar;
    }
    }

/*
Use
*/

$foo = Foo::getInstance();
$foo->incBar();

var_dump($foo->getBar());

$foo = Foo::getInstance();
$foo->incBar();

var_dump($foo->getBar());`

@gemmadlou
Copy link

@technokid - your markdown code isn't styled correctly.

@araeuchle
Copy link

CLASS should be new static()!

@miloslavkostir
Copy link

I use this trait

trait Singleton
{
    private static ?self $instance = null;

    final private function __construct()
    {
        // Singleton
    }

    final public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    final public function __clone()
    {
        throw new \LogicException('Clone is not allowed');
    }

    final public function __wakeup()
    {
        throw new \LogicException('Unserialization is not allowed');
    }
}

then

final class User
{
    use Singleton;
}

$user1 = User::getInstance();
$user2 = User::getInstance();
$user3 = User::getInstance();
  • trait Singleton has only one responsibility: to be single
  • class User has only one responsibility: to represent user
  • everything is final because an extension from a singleton always gets an instance of the extended class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment