Singleton.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Sveta A. Smirnova                          *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: Singleton.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00019     abstract class Singleton
00020     {
00021         private static $instances = array();
00022         
00023         protected function __construct() {/* you can't create me */}
00024         
00026         final public static function getInstance(
00027             $class, $args = null /* , ... */
00028         )
00029         {
00030             // for Singleton::getInstance('class_name', $arg1, ...) calling
00031             if (2 < func_num_args()) {
00032                 $args = func_get_args();
00033                 array_shift($args);
00034             }
00035             
00036             if (!isset(self::$instances[$class])) {
00037                 $object =
00038                     $args
00039                         ? new $class($args)
00040                         : new $class();
00041                 
00042                 Assert::isTrue(
00043                     $object instanceof Singleton,
00044                     "Class '{$class}' is something not a Singleton's child"
00045                 );
00046 
00047                 return self::$instances[$class] = $object;
00048             } else
00049                 return self::$instances[$class];
00050         }
00051         
00052         final public static function getAllInstances()
00053         {
00054             return self::$instances;
00055         }
00056         
00057         final private function __clone() {/* do not clone me */}
00058         final private function __sleep() {/* restless class */}
00059     }
00060 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4