PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms 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. * Access from index.php:
  16. */
  17. if(!defined("_access")) {
  18. die("Error: You don't have permission to access here...");
  19. }
  20. /**
  21. * ZanPHP Singleton Class
  22. *
  23. * This class used to instantiate a class once
  24. *
  25. * @package ZanPHP
  26. * @subpackage core
  27. * @category classes
  28. * @author MilkZoft Developer Team
  29. * @link http://www.zanphp.com/documentation/en/classes/singleton_class
  30. */
  31. class ZP_Singleton {
  32. /**
  33. * Contains the instances of the objects
  34. *
  35. * @var private static $instances = array()
  36. */
  37. public static $instances = array();
  38. /**
  39. * Prevent object cloning
  40. *
  41. * @return void
  42. */
  43. private final function __clone() {}
  44. /**
  45. * Prevent direct object creation
  46. *
  47. * @return void
  48. */
  49. private function __construct() {}
  50. /**
  51. * Returns new or existing Singleton instance
  52. * @param string $class
  53. * @return object value
  54. */
  55. public static function instance($Class, $params = NULL) {
  56. if(is_null($Class)) {
  57. die("Missing class information");
  58. }
  59. if(!array_key_exists($Class, self::$instances)) {
  60. $args = NULL;
  61. $i = 0;
  62. if(is_array($params)) {
  63. foreach($params as $param) {
  64. if($i === count($params) - 1) {
  65. $args .= '"'. $param .'"';
  66. } else {
  67. $args .= '"'. $param .'", ';
  68. }
  69. $i++;
  70. }
  71. }
  72. if(is_null($args)) {
  73. self::$instances[$Class] = new $Class;
  74. } else {
  75. eval("self::\$instances[\$Class] = new \$Class($args);");
  76. }
  77. }
  78. return self::$instances[$Class];
  79. }
  80. }