<?php
/**
* cSingleton 0.1
*
* Created just for fun and learning.
*
* Copyright (C) 2008 Simon István (HUN)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @version 0.1
* @author Simon István (MaDog) <hide@address.com>
* @copyright Copyright (c) 2008, Simon István - this source code published
* under the license named GNU/GPL - use my codes in your own risk!
*/
/**
* Error defines
*/
// Error level
define('ERROR_SINGLETON_NEW', E_USER_ERROR);
define('ERROR_SINGLETON_CLONE', E_USER_ERROR);
// Error text
define('ERRORTEXT_SINGLETON_NEW', 'New is not allowed.');
define('ERRORTEXT_SINGLETON_CLONE', 'Clone is not allowed.');
/**
* class cSingleton
*
* Singleton design patern with factory. Redesign when arrive the PHP 5.3.
*
* @version 0.1
* @author Simon István (MaDog) <hide@address.com>
*/
class cSingleton
{
/**
* Array of singleton class object.
*
* @static
* @access protected
*/
protected static $c_Instance = array();
/**
* Array of singleton class ReflectionObject. This will use later debug or other nice functions.
*
* @static
* @access protected
*/
protected static $c_Reflection = array();
/**
* The protected construct isn't allow create direkt the class object.
*
* @return NULL
* @final
* @access protected
*/
protected final function __construct()
{
// Empty becouse nobody can't use. :)
}
/**
* E_USER_ERROR when try some class clone.
*
* @return NULL
* @final
* @access public
*/
public final function __clone()
{
trigger_error(ERRORTEXT_SINGLETON_CLONE, ERROR_SINGLETON_CLONE);
}
/**
* See has-e a instance, the singleton class.
*
* @return bool
* @static
* @final
* @access public
*/
public static final function hasInstance($p_ClassName)
{
return isset(self::$c_Instance[$p_ClassName]) ? true : false;
}
/**
* Save to the array the singleton class.
*
* @return bool
* @static
* @final
* @access public
*/
public static final function setInstance($p_ClassName, $p_Object)
{
if(!isset(self::$c_Instance[$p_ClassName]))
self::$c_Instance[$p_ClassName] = $p_Object;
if(!isset(self::$c_Reflection[$p_ClassName]))
self::$c_Reflection[$p_ClassName] = new ReflectionObject($p_Object);
}
/**
* Return the object reference. If hasn't have create for use.
*
* @return instance
* @static
* @final
* @access public
*/
public static final function getInstance()
{
$l_Arguments = func_get_args();
$l_ClassName = array_shift($l_Arguments);
if($l_ClassName == '')
return NULL;
if(!isset(self::$c_Instance[$l_ClassName]))
{
self::$c_Reflection[$l_ClassName] = new ReflectionClass($l_ClassName);
self::$c_Instance[$l_ClassName] = self::$c_Reflection[$l_ClassName]->newInstanceArgs($l_Arguments);
}
return self::$c_Instance[$l_ClassName];
}
/**
* If has instance, return the ReflectionClass reference.
*
* @return ReflectionClass
* @static
* @final
* @access public
*/
public static final function getReflection($p_ClassName)
{
if(isset(self::$c_Reflection[$p_ClassName]))
return self::$c_Reflection[$p_ClassName];
else
return NULL;
}
}
/**
* abstract class aSingleton
*
* This abstract class need for singleton class.
*
* @version 0.1
* @author Simon István (MaDog) <hide@address.com>
*/
abstract class aSingleton
{
/**
* The class name.
*
* @static
* @access protected
*/
protected $c_ClassName;
/**
* Only once allowed use the new operator, or we get a E_USER_ERROR.
*
* @final
* @access public
*/
public final function __construct()
{
$this->c_ClassName = get_class($this);
if(cSingleton::hasInstance($this->c_ClassName))
trigger_error(ERRORTEXT_SINGLETON_NEW, ERROR_SINGLETON_NEW);
$l_Arguments = func_get_args();
call_user_func_array(array($this, '__construct__'), $l_Arguments);
cSingleton::setInstance($this->c_ClassName, $this);
}
/**
* Not allowed cloning.
*
* @final
* @access public
*/
public final function __clone()
{
trigger_error(ERRORTEXT_SINGLETON_CLONE, ERROR_SINGLETON_CLONE);
}
/**
* This not work yet.
*
* @return instance
* @static
* @final
* @access public
*/
public static final function getInstance()
{
// Fennt tartva PHP 5.3 részére
// return cSingleton::getInstance(get_called_class());
}
/**
* This is the extended class construct what the cSingleton class call when create
* a instance.
*
* @abstract
* @access protected
*/
abstract protected function __construct__();
}
?>