PluginProbe
Responsive Menu – Mobile Menu & Hamburger Menu Builder / 4.7.4
Responsive Menu – Mobile Menu & Hamburger Menu Builder v4.7.4
4.7.4 4.7.3 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.10 4.1.11 4.1.12 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 All 91 releases
responsive-menu / v4.0.0 / inc / traits / trait-singleton.php

trait-singleton.php in Responsive Menu – Mobile Menu & Hamburger Menu Builder 4.7.4, at v4.0.0/inc/traits/trait-singleton.php

90 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Singleton trait which implements Singleton pattern in any class in which this trait is used.
4 *
5 * Using the singleton pattern in WordPress is an easy way to protect against
6 * mistakes caused by creating multiple objects or multiple initialization
7 * of classes which need to be initialized only once.
8 *
9 * With complex plugins, there are many cases where multiple copies of
10 * the plugin would load, and action hooks would load (and trigger) multiple
11 * times.
12 *
13 * If you're planning on using a global variable, then you should implement
14 * this trait. Singletons are a way to safely use globals; they let you
15 * access and set the global from anywhere, without risk of collision.
16 *
17 * If any method in a class needs to be aware of "state", then you should
18 * implement this trait in that class.
19 *
20 * If any method in the class need to "talk" to another or be aware of what
21 * another method has done, then you should implement this trait in that class.
22 *
23 * If you specifically need multiple objects, then use a normal class.
24 *
25 * @package responsive-menu
26 */
27
28 namespace RMP\Features\Inc\Traits;
29
30 trait Singleton {
31
32 /**
33 * Protected class constructor to prevent direct object creation
34 *
35 * This is meant to be overridden in the classes which implement
36 * this trait. This is ideal for doing stuff that you only want to
37 * do once, such as hooking into actions and filters, etc.
38 */
39 protected function __construct() {
40 }
41
42 /**
43 * Prevent object cloning
44 */
45 final protected function __clone() {
46 }
47
48 /**
49 * This method returns new or existing Singleton instance
50 * of the class for which it is called. This method is set
51 * as final intentionally, it is not meant to be overridden.
52 *
53 * @return object Singleton instance of the class.
54 */
55 final public static function get_instance() {
56
57 /**
58 * Collection of instance.
59 *
60 * @var array
61 */
62 static $instance = array();
63
64 /**
65 * If this trait is implemented in a class which has multiple
66 * sub-classes then static::$_instance will be overwritten with the most recent
67 * sub-class instance. Thanks to late static binding
68 * we use get_called_class() to grab the called class name, and store
69 * a key=>value pair for each `classname => instance` in self::$_instance
70 * for each sub-class.
71 */
72 $called_class = get_called_class();
73
74 if ( ! isset( $instance[ $called_class ] ) ) {
75
76 $instance[ $called_class ] = new $called_class();
77
78 /**
79 * Dependent items can use the RMP_features_singleton_init_{$called_class} hook to execute code
80 */
81 do_action( sprintf( 'rmp_features_singleton_init_%s', $called_class ) );
82
83 }
84
85 return $instance[ $called_class ];
86
87 }
88
89 }
90