addons
6 days ago
general-cleanup
6 days ago
class-adbc-admin-init.php
6 days ago
class-adbc-automation.php
6 days ago
class-adbc-hardcoded-items.php
6 days ago
class-adbc-migration.php
6 days ago
class-adbc-routes.php
6 days ago
class-adbc-scan-counter.php
7 months ago
class-adbc-settings.php
6 days ago
class-adbc-singleton.php
7 months ago
class-adbc-singleton.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC Singleton class. |
| 9 | * |
| 10 | * This class provides the singleton pattern to be used by other classes. |
| 11 | */ |
| 12 | abstract class ADBC_Singleton { |
| 13 | |
| 14 | /** |
| 15 | * Holds the instance of each subclass. |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | private static $_instances = []; |
| 20 | |
| 21 | /** |
| 22 | * Constructor. |
| 23 | */ |
| 24 | protected function __construct( ...$args ) { |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Get the singleton instance. |
| 29 | * |
| 30 | * @return object The singleton instance. |
| 31 | */ |
| 32 | public static function instance( ...$args ) { |
| 33 | |
| 34 | $cls = static::class; |
| 35 | |
| 36 | if ( ! isset( self::$_instances[ $cls ] ) ) { |
| 37 | self::$_instances[ $cls ] = new static( ...$args ); |
| 38 | } |
| 39 | |
| 40 | return self::$_instances[ $cls ]; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Prevent cloning. |
| 45 | */ |
| 46 | final public function __clone() { |
| 47 | throw new Exception( "Cannot clone a singleton." ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Prevent unserialization for PHP < 7.4 |
| 52 | */ |
| 53 | final public function __wakeup() { |
| 54 | throw new Exception( "Cannot unserialize a singleton." ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Prevent serialization for PHP 7.4+ |
| 59 | */ |
| 60 | final public function __serialize() { |
| 61 | throw new Exception( "Cannot serialize a singleton." ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Prevent unserialization for PHP 7.4+ |
| 66 | */ |
| 67 | final public function __unserialize( $data ) { |
| 68 | throw new Exception( "Cannot unserialize a singleton." ); |
| 69 | } |
| 70 | |
| 71 | } |