back to devpro   download
<?php
// (C) Andrea Giammarchi - Mit Style License
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(){
        trigger_error(__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();
    }
}
?>
back to devpro   download
Creative Commons License