I try to implement a singleton pattern with the capability of passing arguments to the instructor. For the singleton pattern I want to create a base class. The main problem with this is that not all derived classes have the same amount of parameters. For the singleton base class I have this so far:
abstract class Singleton
{
/**
* Function: __construct
* Description: the function which creates the object.
*/
protected function __construct()
{
}
private final function __clone()
{
}
public static function getInstance()
{
$args = func_get_args();
static $instance = null;
return $instance = $instance ? : new static();
}
}
So far, this works, but I can't pass arguments to the constructor with this. Of course I can directly pass the $args array to the constructor, but I don't want to have to unfold the array in each constructor of the derived classes, I want to be able to pass the arguments as normal parameters.
I've tried several things already: I tried using call_user_func_array, but I couldn't figure out how to construct an object with this function (if it's possible at all), and I've also tried using the ReflectionClass. The problem with the ReflectionClass was that this class can't access the constructor since the constructor is protected.
So does anyone has any ideas for me how to solve this?
ps. sorry if I'm acting difficult, but I just try to find the best solution (and understand that solution).