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