| 1 |
<?php |
| 2 |
namespace Imagify\Traits; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Trait that simulates a singleton pattern. |
| 8 |
* The idea is more to ease the instance retrieval than to prevent multiple instances. |
| 9 |
* This is temporary, until we get a DI container. |
| 10 |
* |
| 11 |
* @since 1.9 |
| 12 |
* @since 1.9.4 Renamed into InstanceGetterTrait. |
| 13 |
* @author Grégory Viguier |
| 14 |
*/ |
| 15 |
trait InstanceGetterTrait { |
| 16 |
|
| 17 |
/** |
| 18 |
* The "not-so-single" instance of the class. |
| 19 |
* |
| 20 |
* @var object |
| 21 |
* @since 1.9 |
| 22 |
* @access protected |
| 23 |
* @author Grégory Viguier |
| 24 |
*/ |
| 25 |
protected static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the main Instance. |
| 29 |
* |
| 30 |
* @since 1.9 |
| 31 |
* @access protected |
| 32 |
* @author Grégory Viguier |
| 33 |
* |
| 34 |
* @return object Main instance. |
| 35 |
*/ |
| 36 |
public static function get_instance() { |
| 37 |
if ( ! isset( static::$instance ) ) { |
| 38 |
static::$instance = new static(); |
| 39 |
} |
| 40 |
|
| 41 |
return static::$instance; |
| 42 |
} |
| 43 |
} |
| 44 |
|