| 1 |
<?php |
| 2 |
/** |
| 3 |
* Singleton that can be used inside abstract classes. |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @since StoreEngine v1.6.7 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace StoreEngine\Traits; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
trait AbstractSingleton { |
| 16 |
|
| 17 |
protected static array $instances = []; |
| 18 |
|
| 19 |
public static function init() { |
| 20 |
return self::get_instance(); |
| 21 |
} |
| 22 |
|
| 23 |
public static function get_instance() { |
| 24 |
$called = static::class; |
| 25 |
|
| 26 |
if ( ! isset( self::$instances[ $called ] ) ) { |
| 27 |
self::$instances[ $called ] = new $called(); |
| 28 |
} |
| 29 |
|
| 30 |
return self::$instances[ $called ]; |
| 31 |
} |
| 32 |
|
| 33 |
protected function __construct() { |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Cloning is forbidden. |
| 38 |
*/ |
| 39 |
public function __clone() { |
| 40 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cloning is forbidden.', 'storeengine' ), '1.6.7' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Unserializing instances of this class is forbidden. |
| 45 |
*/ |
| 46 |
public function __wakeup() { |
| 47 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Unserializing instances of this class is forbidden.', 'storeengine' ), '1.6.7' ); |
| 48 |
} |
| 49 |
} |
| 50 |
|