Singleton()

Description #

A class to be used as a base for all singleton classes.

Changelog #

VersionDescription
4.1.8Introduced.

Source #

File: includes/class/class-singleton.php

abstract class Singleton {

	/**
	 * Cloning is forbidden.
	 *
	 * @return void
	 * @since 4.1.8
	 */
	private function __clone() {
		_doing_it_wrong( __FUNCTION__, esc_attr__( 'Cheatin’ huh?', 'anspress-question-answer' ), '1.0.0' );
	}

	/**
	 * Unserializing instances of this class is forbidden.
	 *
	 * @since 4.1.8
	 * @since 4.2.0 Fixed: warning `__wakeup must be public`.
	 */
	public function __wakeup() {
		_doing_it_wrong( __FUNCTION__, esc_attr__( 'Cheatin’ huh?', 'anspress-question-answer' ), '1.0.0' );
	}

	/**
	 * Creates or returns an instance of this class.
	 *
	 * @return AnsPress\Singleton A single instance of this class.
	 * @since 4.1.8
	 */
	public static function init() {
		if ( is_null( static::$instance ) ) {
			static::$instance = new static();
			static::$instance->run_once();
		}

		return static::$instance;
	}

	/**
	 * Placeholder function which is called only once.
	 *
	 * @return void
	 * @since 4.1.8
	 */
	public function run_once() {
	}
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment