class-singleton.php
1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Singleton class for all classes.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\Core\Utils\Abstracts
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\Core\Utils\Abstracts;
// Abort if called directly.
defined( 'WPINC' ) || die;
/**
* Class Singleton
*
* @package WPMUDEV_BLC\Core\Utils\Abstracts
*/
abstract class Singleton {
/**
* Singleton constructor.
*
* Protect the class from being initiated multiple times.
*
* @param array $props Optional properties array.
*
* @since 2.0.0
*/
protected function __construct( $props = array() ) {
// Protect class from being initiated multiple times.
}
/**
* Instance obtaining method.
*
* @return static Called class instance.
* @since 2.0.0
*/
public static function instance() {
static $instances = array();
// @codingStandardsIgnoreLine Plugin-backported
$called_class_name = get_called_class();
if ( ! isset( $instances[ $called_class_name ] ) ) {
$instances[ $called_class_name ] = new $called_class_name();
}
return $instances[ $called_class_name ];
}
}