Singleton.php
53 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Singleton class trait. |
| 4 | * |
| 5 | * @package EverestForms\Traits |
| 6 | */ |
| 7 | |
| 8 | namespace EverestForms\Traits; |
| 9 | |
| 10 | /** |
| 11 | * Singleton trait. |
| 12 | */ |
| 13 | trait Singleton { |
| 14 | |
| 15 | /** |
| 16 | * Holds single instance of the class. |
| 17 | * |
| 18 | * @var null|static |
| 19 | */ |
| 20 | private static $instance = null; |
| 21 | |
| 22 | /** |
| 23 | * Get instance of the class. |
| 24 | * |
| 25 | * @return static |
| 26 | */ |
| 27 | final public static function init() { |
| 28 | if ( is_null( static::$instance ) ) { |
| 29 | static::$instance = new static(); |
| 30 | } |
| 31 | return static::$instance; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | */ |
| 37 | protected function __construct() {} |
| 38 | |
| 39 | /** |
| 40 | * Disable un-serializing of the class. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function __wakeup() {} |
| 45 | |
| 46 | /** |
| 47 | * Disable cloning of the class. |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | public function __clone() {} |
| 52 | } |
| 53 |