dimanche 31 juillet 2022

How to properly extend singleton class?

I have a singleton class from vendor

class vendorSingleton
{
    protected static $instance = null;
 
    public static function getInstance() {
        if (!static::$instance) {
            static::$instance = new static();
        }

        return static::$instance;
    }

    ... some internal logic and getters/setters ...  
}

I need to extend it. Problem here is that both child and a parent MUST share the same instance, because its state is changed during request and is important for application logic. Also, both child and parent ::getInstance will be called, because I can't update all places so that they use child class directly. Currently I came up with this implementation and it works

class childSingleton extends vendorSingleton
{
    /**
     * @return static
     */
    public static function getInstance()
    {
        $instance = new static();
        $instanceReflection = new ReflectionClass($instance);
        $parentInstanceReflection = new ReflectionClass(parent::getInstance());

        foreach ($parentInstanceReflection->getProperties() as $parentProperty) {
            $parentProperty->setAccessible(true);

            $property = $instanceReflection->getProperty($parentProperty->getName());

            $property->setAccessible(true);
            $property->setValue($instance, $parentProperty->getValue(parent::getInstance()));
        }

        static::$instance = $instance;

        return static::$instance;
    }

    ... new method ...
}

But I don't really like it since it uses reflections and overall looks pretty error prone. Could this be done using other, better approach?





Aucun commentaire:

Enregistrer un commentaire