trait-singleton.php
| 1 | <?php |
| 2 | /** |
| 3 | * Singleton trait. |
| 4 | * |
| 5 | * @package wp-job-manager-alerts |
| 6 | */ |
| 7 | |
| 8 | namespace WP_Job_Manager; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | trait Singleton { |
| 15 | |
| 16 | /** |
| 17 | * The single instance of the class. |
| 18 | * |
| 19 | * @var self |
| 20 | */ |
| 21 | private static $instance = null; |
| 22 | |
| 23 | /** |
| 24 | * Allows for accessing single instance of class. Class should only be constructed once per call. |
| 25 | * |
| 26 | * @since 2.2.0 |
| 27 | * @static |
| 28 | * @return self Main instance. |
| 29 | */ |
| 30 | public static function instance() { |
| 31 | if ( is_null( self::$instance ) ) { |
| 32 | self::$instance = new self(); |
| 33 | } |
| 34 | |
| 35 | return self::$instance; |
| 36 | } |
| 37 | |
| 38 | } |
| 39 |