# tableberg/0.3.3/includes/traits/Singleton_Trait.php

Tableberg – Simple Gutenberg Table Block, version 0.3.3. 74 lines.

- Page: https://pluginprobe.com/plugins/tableberg/0.3.3/code/includes/traits/Singleton_Trait.php
- Raw: https://pluginprobe.com/plugins/tableberg/0.3.3/raw/includes/traits/Singleton_Trait.php
- Modified: 2024-01-09T18:32:24+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/tableberg/0.3.3/code/includes/traits/Singleton_Trait.php#L10-L20`.

```php
<?php
/**
 * Singleton trait
 *
 * @package Tableberg
 */

namespace Tableberg\includes\traits;

/**
 * Singleton trait.
 */
trait Singleton_Trait {
	/**
	 * Class instance options.
	 *
	 * @private
	 * @var array
	 */
	private $class_options = array();

	/**
	 * Manager base instance.
	 *
	 * @var null|object
	 */
	protected static $instance = null;

	/**
	 * Class instance constructor.
	 *
	 * @param array $const_args constructor args.
	 */
	protected function __construct( $const_args = array() ) {
		$this->class_options = $const_args;
	}

	/**
	 * Get class instance.
	 *
	 * @param array       $constructor_args constructor args.
	 * @param string|null $class_name class name to create instance.
	 *
	 * @return object object instance
	 */
	public static function get_instance( $constructor_args = array(), $class_name = null ) {
		if ( is_null( static::$instance ) ) {
			static::create_instance( $constructor_args, $class_name );
		}

		return static::$instance;
	}

	/**
	 * Force create instance of singleton if it there is none.
	 *
	 * @param array       $constructor_args constructor args.
	 * @param string|null $class_name class name to create instance.
	 *
	 * @return void
	 */
	final protected static function create_instance( $constructor_args = array(), $class_name = null ) {
		if ( is_null( static::$instance ) ) {
			$class_name = __CLASS__;

			if ( ! is_null( $class_name ) ) {
				$class_name = class_exists( $class_name ) ? $class_name : __CLASS__;
			}

			static::$instance = new $class_name( $constructor_args );
		}
	}
}

```
