SingletonTrait.php
53 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Singleton class trait. |
| 4 | * |
| 5 | * @package WooCommerce\Utilities |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\RestApi\Utilities; |
| 9 | |
| 10 | /** |
| 11 | * Singleton trait. |
| 12 | */ |
| 13 | trait SingletonTrait { |
| 14 | /** |
| 15 | * The single instance of the class. |
| 16 | * |
| 17 | * @var object |
| 18 | */ |
| 19 | protected static $instance = null; |
| 20 | |
| 21 | /** |
| 22 | * Constructor |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | protected function __construct() {} |
| 27 | |
| 28 | /** |
| 29 | * Get class instance. |
| 30 | * |
| 31 | * @return object Instance. |
| 32 | */ |
| 33 | final public static function instance() { |
| 34 | if ( null === static::$instance ) { |
| 35 | static::$instance = new static(); |
| 36 | } |
| 37 | return static::$instance; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Prevent cloning. |
| 42 | */ |
| 43 | private function __clone() {} |
| 44 | |
| 45 | /** |
| 46 | * Prevent unserializing. |
| 47 | */ |
| 48 | final public function __wakeup() { |
| 49 | wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'woocommerce' ), '4.6' ); |
| 50 | die(); |
| 51 | } |
| 52 | } |
| 53 |