Skip to content

Instantly share code, notes, and snippets.

@sergeliatko
Last active April 11, 2018 14:15
Show Gist options
  • Save sergeliatko/58c33cadbc88ea00e5af197eb38a3492 to your computer and use it in GitHub Desktop.
Save sergeliatko/58c33cadbc88ea00e5af197eb38a3492 to your computer and use it in GitHub Desktop.
Singleton trait
<?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