01-06-2009, 03:15 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Jan 2009
Posts: 40
Thanks: 10
|
Sorry for the double post but i wanted to show what code I have been using to make it global untill now.
PHP Code:
function Factory($__CLASS__)
{
static $list = array();
if(!isset($list[$__CLASS__]))
{
$list[$__CLASS__] = new ReflectionClass($__CLASS__);
}
$arguments = func_get_args();
array_shift($arguments);
return $list[$__CLASS__]->getConstructor() ? $list[$__CLASS__]->newInstanceArgs($arguments) : $list[$__CLASS__]->newInstance();
}
function Singleton($__CLASS__)
{
static $list = array();
if(!isset($list[$__CLASS__]))
{
$arguments = func_get_args();
$list[$__CLASS__] = call_user_func_array('Factory', $arguments);
}
return $list[$__CLASS__];
}
class Singleton
{
private $_class,$_instance;
public function __construct($__CLASS__)
{
$arguments = func_get_args();
$this->_class = new ReflectionClass($this->_instance = call_user_func_array(__CLASS__, $arguments));
}
public function __get($property)
{
return $this->_instance->$property;
}
public function __call($method, array $arguments)
{
return $this->_class->getMethod($method)->invokeArgs($this->_instance, $arguments);
}
public function __set($property, $value)
{
$this->_instance->$property = $value;
}
public function __clone()
{
die(__CLASS__.' cannot be cloned');
}
public function equal($_instance)
{
return $_instance instanceof Singleton ? $this->_instance === $_instance->_instance : $this->_instance === $_instance;
}
}
class Factory extends Singleton
{
protected $_arguments;
public function __construct($__CLASS__)
{
$this->_arguments = func_get_args();
$this->_class = new ReflectionClass($this->_instance = call_user_func_array(__CLASS__, $this->_arguments));
array_shift($this->_arguments);
}
public function __clone()
{
return 0 < count($this->_arguments) ? $this->_class->newInstanceArgs($this->_arguments) : $this->_class->newInstance();
}
}
It works perfect, I can use it inside all functions and classes and anywhere on the site but its just not very nice to use. It needs to be simple for people to call it etc when they use this.
Example usage of above:
PHP Code:
echo Factory('app')->xss_clean('f);
|
|
|
|