<?php
/**
 * singleton class
 *
 * using example:
 * class mysingleton extends singleton {
 *    // php singleton wrapper
 *    static public function __getMe( ) { return parent::__getMe( __CLASS__ ); }
 *     // your methods ...
 * }
 *
 * // next everywhere in code just
 * // use __getMe static method of your class
 * // to get unique instance of your class
 * mysingleton::__getMe( ); // returns instance;
 *
 *
 * @version:    0.1a
 * @package:    flame.core
 * @author:    alex_ez
 * @created:    11 nov 2008
 * @link:    http://src.qfox.ru/
 *
 * @copyright:    (c) 2008 flamebox.ru. All rights reserved.
 *
 */

/** standart verifying */
if( !defined'4KCQ' ) ) return;

if( 
defined'__SINGLETON.CLASS_PHP' ) ) return;
define'__SINGLETON.CLASS_PHP''0.1' );

class 
singleton
{
    
/**
     * abstract constructor
     */
    
private function __construct( ) { throw new Exception"You must describe private constructor." ); }
    
    private static 
$instances = array( );
    
    
/**
     * singleton generator
     *
     * @access public
     * @static
     * @param    String    __CLASS__
     * @return    unique instance of class
     */
    
static public function __getMe( )
    {
        
$c func_num_args() ? func_get_arg(0) : null;
        
        if( 
is_null$c ) ) { throw new Exception"You must specify static public method __getMe(){ return parent::__getMe(__CLASS__); } method in your class." ); }
        
        if( !
is_arrayself::$instances ) )
            
self::$instances = array( );
        
        if( !
array_key_exists($cself::$instances) )
            
self::$instances[$c] = new $c( );
        
        return 
self::$instances[$c];
    }
    
    static public function 
classes( )
    {
        return 
array_keysself::$instances );
    }
    
    static public function 
get$c null )
    {
        return ( !
is_null($c) && array_key_exists($c,self::$instances) )
            ? 
self::$instances[$c]
            : 
self::$instances;
    }
    
    static public function 
destroy$c )
    {
        if( !
array_key_exists($cself::$instances) )
            return 
false;
        
        unset( 
self::$instances[$c] );
        return 
true;
    }
    
    public function 
__clone() { throw new Exception"Clone is not allowed." ); }
    
}
 
?>