Skip to content

Instantly share code, notes, and snippets.

@kjbenk
Created January 29, 2018 16:32
Show Gist options
  • Save kjbenk/74a978f75b9e374fa8cfaa500e5ac0d6 to your computer and use it in GitHub Desktop.
Save kjbenk/74a978f75b9e374fa8cfaa500e5ac0d6 to your computer and use it in GitHub Desktop.
WordPress Singleton Trait
<?php
/**
* Trait file for Singletons.
*/
/**
* Make a class into a singleton.
*/
trait Singleton {
/**
* Existing instance.
*
* @var array
*/
protected static $instance;
/**
* Get class instance.
*
* @return object
*/
public static function instance() {
if ( ! isset( static::$instance ) ) {
static::$instance = new static();
static::$instance->setup();
}
return static::$instance;
}
/**
* Setup the singleton.
*/
public function setup() {
// Silence
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment