| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* Authorizer |
| 4 |
* |
| 5 |
* @license GPL-2.0+ |
| 6 |
* @link https://github.com/uhm-coe/authorizer |
| 7 |
* @package authorizer |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Authorizer; |
| 11 |
|
| 12 |
/** |
| 13 |
* Base class that all other classes extend (provides static accessor variable). |
| 14 |
*/ |
| 15 |
abstract class Singleton { |
| 16 |
/** |
| 17 |
* Instances of any child classes. |
| 18 |
* |
| 19 |
* @var object[] Array of objects of any instantiated child classes. |
| 20 |
*/ |
| 21 |
private static $instances = array(); |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Access the singleton instance of the requested class (create a new one if |
| 26 |
* needed). |
| 27 |
* |
| 28 |
* @return object Object of the requested class. |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
$class = get_called_class(); |
| 32 |
if ( ! isset( self::$instances[ $class ] ) ) { |
| 33 |
self::$instances[ $class ] = new static(); |
| 34 |
} |
| 35 |
|
| 36 |
return self::$instances[ $class ]; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Disable constructor to prevent creation of multiple instances (protected |
| 42 |
* so we can create a new instance within get_instance() though). |
| 43 |
*/ |
| 44 |
protected function __construct() { |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Disable cloning of singletons. |
| 49 |
*/ |
| 50 |
private function __clone() { |
| 51 |
} |
| 52 |
} |
| 53 |
|