# learnpress/4.1.7.2/inc/abstracts/abstract-singleton.php

LearnPress – WordPress LMS Plugin for Create and Sell Online Courses, version 4.1.7.2. 58 lines.

- Page: https://pluginprobe.com/plugins/learnpress/4.1.7.2/code/inc/abstracts/abstract-singleton.php
- Raw: https://pluginprobe.com/plugins/learnpress/4.1.7.2/raw/inc/abstracts/abstract-singleton.php
- Modified: 2022-09-23T02:52:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/learnpress/4.1.7.2/code/inc/abstracts/abstract-singleton.php#L10-L20`.

```php
<?php
/**
 * Class LP_Abstract_Singleton
 */
abstract class LP_Abstract_Singleton {

	/**
	 * Array of singleton classes.
	 *
	 * @var array
	 */
	protected static $instances = array();

	/**
	 * LP_Abstract_Singleton constructor.
	 */
	protected function __construct() {
	}

	/**
	 * @return mixed
	 */
	public static function instance() {
		$name = self::_get_called_class();

		if ( false === $name ) {
			return false;
		}

		if ( empty( self::$instances[ $name ] ) ) {
			self::$instances[ $name ] = new $name();
		}

		return self::$instances[ $name ];
	}

	/**
	 * @return bool|string
	 */
	protected static function _get_called_class() {
		if ( function_exists( 'get_called_class' ) ) {
			return get_called_class();
		}

		$backtrace = debug_backtrace();

		if ( empty( $backtrace[2] ) ) {
			return false;
		}

		if ( empty( $backtrace[2]['args'][0] ) ) {
			return false;
		}

		return $backtrace[2]['args'][0];
	}
}

```
