# Singleton.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2004-2009 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
    00018     abstract class Singleton
    00019     {
    00020         private static $instances = array();
    00021
    00022         protected function __construct() {/* you can't create me */}
    00023
    00025         final public static function getInstance(
    00026             $class, $args = null /* , ... */
    00027         )
    00028         {
    00029             if (!isset(self::$instances[$class])) {
    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                     // can't call protected constructor through reflection
    00036                     eval(
    00037                         '$object = new '.$class
    00038                         .'($args['.implode('],$args[', array_keys($args)).']);'
    00039                     );
    00040
    00041                 } else {
    00042                     $object =
    00043                         $args
    00044                             ? new $class($args)
    00045                             : new $class();
    00046                 }
    00047
    00048                 Assert::isTrue(
    00049                     $object instanceof Singleton,
    00050                     "Class '{$class}' is something not a Singleton's child"
    00051                 );
    00052
    00053                 self::$instances[$class] = $object;
    00054             }
    00055
    00056             return self::$instances[$class];
    00057         }
    00058
    00059         final public static function getAllInstances()
    00060         {
    00061             return self::$instances;
    00062         }
    00063
    00064         /* void */ final public static function dropInstance($class)
    00065         {
    00066             if (!isset(self::$instances[$class]))
    00067                 throw new MissingElementException('knows nothing about '.$class);
    00068
    00069             unset(self::$instances[$class]);
    00070         }
    00071
    00072         final private function __clone() {/* do not clone me */}
    00073         final private function __sleep() {/* restless class */}
    00074     }
    00075 ?>