PageRenderTime 143ms CodeModel.GetById 81ms app.highlight 2ms RepoModel.GetById 58ms app.codeStats 0ms

/zan/classes/singleton.php

https://github.com/triartdesign/ZanPHP
PHP | 92 lines | 76 code | 1 blank | 15 comment | 1 complexity | 582e1fd3b4a451db9fef5f84c40f4bd0 MD5 | raw file
 1<?php
 2/**
 3 * ZanPHP
 4 *
 5 * An open source agile and rapid development framework for PHP 5
 6 *
 7 * @package		ZanPHP
 8 * @author		MilkZoft Developer Team
 9 * @copyright	Copyright (c) 2011, MilkZoft, Inc.
10 * @license		http://www.zanphp.com/documentation/en/license/
11 * @link		http://www.zanphp.com
12 * @version		1.0
13 */
14 
15/**
16 * Access from index.php:
17 */
18if(!defined("_access")) {
19	die("Error: You don't have permission to access here...");
20}
21
22/**
23 * ZanPHP Singleton Class
24 *
25 * This class used to instantiate a class once
26 *
27 * @package		ZanPHP
28 * @subpackage	core
29 * @category	classes
30 * @author		MilkZoft Developer Team
31 * @link		http://www.zanphp.com/documentation/en/classes/singleton_class
32 */
33class ZP_Singleton {
34	
35    /**
36     * Contains the instances of the objects
37     *
38     * @var private static $instances = array()
39     */	
40	public static $instances = array();
41	
42	/**
43     * Prevent object cloning
44     *
45     * @return void
46     */
47	private final function __clone() {}
48
49    /**
50     * Prevent direct object creation
51     *
52     * @return void
53     */	
54	private function __construct() {}
55	
56	/**
57     * Returns new or existing Singleton instance
58     * @param string $class
59     * @return object value
60     */
61	public static function instance($Class, $params = NULL) {
62		if(is_null($Class)) {
63			die("Missing class information");
64		}
65
66		if(!array_key_exists($Class, self::$instances)) {	
67			$args = NULL;
68			
69			$i = 0;
70			if(is_array($params)) {
71				foreach($params as $param) {
72					if($i === count($params) - 1) {
73						$args .= '"'. $param .'"';
74					} else {
75						$args .= '"'. $param .'", ';
76					}
77					
78					$i++;
79				}
80			}
81			
82			if(is_null($args)) {
83				self::$instances[$Class] = new $Class;
84			} else {
85				eval("self::\$instances[\$Class] = new \$Class($args);");
86			}
87		}
88		
89		return self::$instances[$Class];
90	}
91
92}