Last active
April 11, 2018 14:15
-
-
Save sergeliatko/58c33cadbc88ea00e5af197eb38a3492 to your computer and use it in GitHub Desktop.
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 Singleton { | |
/** | |
* @var static $instance | |
*/ | |
protected static $instance; | |
/** | |
* @return static | |
*/ | |
public static function getInstance() { | |
if ( empty( self::$instance ) ) { | |
self::setInstance( new self() ); | |
} | |
return self::$instance; | |
} | |
/** | |
* @param static $instance | |
*/ | |
protected static function setInstance( $instance ) { | |
self::$instance = $instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment