| 1 |
<?php |
| 2 |
namespace YayMail\Utils; |
| 3 |
|
| 4 |
trait SingletonTrait { |
| 5 |
private static $instance; |
| 6 |
|
| 7 |
public static function get_instance( ...$args ) { |
| 8 |
$class = get_called_class(); |
| 9 |
if ( ! $class::$instance ) { |
| 10 |
$class::$instance = new $class( ...$args ); |
| 11 |
} |
| 12 |
|
| 13 |
return $class::$instance; |
| 14 |
} |
| 15 |
|
| 16 |
/** Singletons should not be cloneable. */ |
| 17 |
protected function __clone() { } |
| 18 |
|
| 19 |
/** Singletons should not be restorable from strings. */ |
| 20 |
public function __wakeup() { |
| 21 |
throw new \Exception( 'Cannot unserialize a singleton.' ); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
|