| @@ -1,0 +1,60 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * GetInstance File | |
| 4 | + * | |
| 5 | + * @package NotificationX | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace NotificationX; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Base trait make the instances of called class. | |
| 12 | + */ | |
| 13 | +trait GetInstance { | |
| 14 | + /** | |
| 15 | + * Instance of Called Class. | |
| 16 | + * | |
| 17 | + * @var GetInstance | |
| 18 | + */ | |
| 19 | + protected static $instance = null; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Get the instance of called class. | |
| 23 | + * | |
| 24 | + */ | |
| 25 | + public static function get_instance($args = null){ | |
| 26 | + $class = get_called_class(); | |
| 27 | + if ( is_null( static::$instance ) || ! static::$instance instanceof self || (strpos($class, "NotificationXPro\\") === 0 && ! static::$instance instanceof $class) ) { | |
| 28 | + $class = __CLASS__; | |
| 29 | + if(strpos($class, "NotificationX\\") === 0){ | |
| 30 | + $pro_class = str_replace("NotificationX\\", "NotificationXPro\\", $class); | |
| 31 | + if(class_exists($pro_class) && is_subclass_of($pro_class, $class)){ | |
| 32 | + $class = $pro_class; | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | + if(!empty($args)){ | |
| 37 | + static::$instance = new $class($args); | |
| 38 | + } | |
| 39 | + else{ | |
| 40 | + static::$instance = new $class; | |
| 41 | + } | |
| 42 | + } | |
| 43 | + return static::$instance; | |
| 44 | + } | |
| 45 | + | |
| 46 | + // public function __call($name, $arguments){ | |
| 47 | + // $class = __CLASS__ . 'Pro'; | |
| 48 | + | |
| 49 | + // if(strpos($class, "NotificationX\\") === 0){ | |
| 50 | + // $pro_class = str_replace("NotificationX\\", "NotificationXPro\\", $class); | |
| 51 | + // if(class_exists($pro_class)){ | |
| 52 | + // $obj = $pro_class::get_instance(); | |
| 53 | + // if($obj && method_exists($obj, $name)){ | |
| 54 | + // $obj->$name($arguments); | |
| 55 | + // } | |
| 56 | + // } | |
| 57 | + // } | |
| 58 | + // } | |
| 59 | + | |
| 60 | +} | |