| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Abstract_Singleton |
| 4 |
*/ |
| 5 |
abstract class LP_Abstract_Singleton { |
| 6 |
|
| 7 |
/** |
| 8 |
* Array of singleton classes. |
| 9 |
* |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
protected static $instances = array(); |
| 13 |
|
| 14 |
/** |
| 15 |
* LP_Abstract_Singleton constructor. |
| 16 |
*/ |
| 17 |
protected function __construct() { |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* @return mixed |
| 22 |
*/ |
| 23 |
public static function instance() { |
| 24 |
$name = self::_get_called_class(); |
| 25 |
|
| 26 |
if ( false === $name ) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
if ( empty( self::$instances[ $name ] ) ) { |
| 31 |
self::$instances[ $name ] = new $name(); |
| 32 |
} |
| 33 |
|
| 34 |
return self::$instances[ $name ]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @return bool|string |
| 39 |
*/ |
| 40 |
protected static function _get_called_class() { |
| 41 |
if ( function_exists( 'get_called_class' ) ) { |
| 42 |
return get_called_class(); |
| 43 |
} |
| 44 |
|
| 45 |
$backtrace = debug_backtrace(); |
| 46 |
|
| 47 |
if ( empty( $backtrace[2] ) ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
if ( empty( $backtrace[2]['args'][0] ) ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
return $backtrace[2]['args'][0]; |
| 56 |
} |
| 57 |
} |
| 58 |
|