| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wpxero\Marqueex\Traits; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Singleton Trait |
| 11 |
* |
| 12 |
* Provides singleton functionality to classes |
| 13 |
*/ |
| 14 |
trait Singleton { |
| 15 |
/** |
| 16 |
* Instance of this class |
| 17 |
* |
| 18 |
* @var static |
| 19 |
*/ |
| 20 |
protected static $instance = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get instance of this class |
| 24 |
* |
| 25 |
* @return static |
| 26 |
*/ |
| 27 |
public static function get_instance() { |
| 28 |
if (null === static::$instance) { |
| 29 |
static::$instance = new static(); |
| 30 |
} |
| 31 |
|
| 32 |
return static::$instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Prevent cloning of the instance |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
private function __clone() { |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Prevent unserializing of the instance |
| 45 |
* |
| 46 |
* @throws \Exception When attempting to unserialize |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function __wakeup() { |
| 50 |
throw new \Exception('Cannot unserialize singleton'); |
| 51 |
} |
| 52 |
} |
| 53 |
|