Created
January 29, 2018 16:32
-
-
Save kjbenk/74a978f75b9e374fa8cfaa500e5ac0d6 to your computer and use it in GitHub Desktop.
WordPress Singleton Trait
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 | |
/** | |
* 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