ZanPHP /zan/classes/singleton.php

Language PHP Lines 93
MD5 Hash 582e1fd3b4a451db9fef5f84c40f4bd0
Repository https://github.com/triartdesign/ZanPHP.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
 * ZanPHP
 *
 * An open source agile and rapid development framework for PHP 5
 *
 * @package		ZanPHP
 * @author		MilkZoft Developer Team
 * @copyright	Copyright (c) 2011, MilkZoft, Inc.
 * @license		http://www.zanphp.com/documentation/en/license/
 * @link		http://www.zanphp.com
 * @version		1.0
 */
 
/**
 * Access from index.php:
 */
if(!defined("_access")) {
	die("Error: You don't have permission to access here...");
}

/**
 * ZanPHP Singleton Class
 *
 * This class used to instantiate a class once
 *
 * @package		ZanPHP
 * @subpackage	core
 * @category	classes
 * @author		MilkZoft Developer Team
 * @link		http://www.zanphp.com/documentation/en/classes/singleton_class
 */
class ZP_Singleton {
	
    /**
     * Contains the instances of the objects
     *
     * @var private static $instances = array()
     */	
	public static $instances = array();
	
	/**
     * Prevent object cloning
     *
     * @return void
     */
	private final function __clone() {}

    /**
     * Prevent direct object creation
     *
     * @return void
     */	
	private function __construct() {}
	
	/**
     * Returns new or existing Singleton instance
     * @param string $class
     * @return object value
     */
	public static function instance($Class, $params = NULL) {
		if(is_null($Class)) {
			die("Missing class information");
		}

		if(!array_key_exists($Class, self::$instances)) {	
			$args = NULL;
			
			$i = 0;
			if(is_array($params)) {
				foreach($params as $param) {
					if($i === count($params) - 1) {
						$args .= '"'. $param .'"';
					} else {
						$args .= '"'. $param .'", ';
					}
					
					$i++;
				}
			}
			
			if(is_null($args)) {
				self::$instances[$Class] = new $Class;
			} else {
				eval("self::\$instances[\$Class] = new \$Class($args);");
			}
		}
		
		return self::$instances[$Class];
	}

}
Back to Top