Is there any difference between keeping a static instance in a class method vs a class

When working with the singleton template. Is there a difference when storing a static instance in a class and holding it in a method that returns an instance?

Examples: Inside a class.

class cExampleA {
    static $mInstance;

    protected function __construct() {
        /* Protected so only the class can instantiate. */
    }

    static public function GetInstance() {
        return (is_object(self::$mInstance) ? self::$mInstance : self::$mInstance = new self());
    }
}

      

Inside the return method.

class cExampleB {
    protected function __construct() {
        /* Protected so only the class can instantiate. */
    }

    static public function GetInstance() {
        static $smInstance;
        return (is_object($smInstance) ? $smInstance : $smInstance = new self());
    }
}

      

On the other hand, is using the ternary operator valid in the example (which might cause problems) and is there any advantage / downside when using is_object over isset?

Update. It seems that the only difference is, what is the scope of a static instance?

+3


source to share


2 answers


Interest Ask. As far as behavior is concerned, as far as I know, there is no difference between these two examples (other than scope).

PHP is a loosely typed programming language, so you can even omit the class property declaration all together in example A. And with a weakly typed language, there are always more ways to achieve the same!

The real problem will soon become one of the design patterns (data encapsulation in this case) to ensure that we write code that follows a convention developed by people much smarter than I!

In this example, the class must own its property and set the scope of that property appropriately (in this case, it is private) and provide boolean accessors.



This is how I wrote it:

class ExampleA
{
    static private $_instance = null;

    private function __construct(){}

    static public function getInstance()
    {
        if (!self::$_instance instanceof self) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

      

If you were to wrap the variable in the method itself, strictly speaking, it won't be a property of the class. Also, you lose a bit of readability - this is the feeling that other developers (not used for the design pattern from example B) will be looking for a class property declaration rather than digging through the class methods.

+1


source


Is there a difference between holding a static instance in a class and holding it in a method that returns an instance?

No, there is no difference in functionality if you change the visibility of a static instance property to both protected and private:

class cExampleA {
    protected static $instance = null;
    // ...
}

      

Btw, don't use type designations $mInstance

; you don't need these crutches currently



Also, after seeing how you set the constructor to protected visibility, I am guessing you might want to extend the class later? In this case, you will need to change the method getInstance()

:

public final static function GetInstance() 
{
    if (self::$instance === null) {
        self::$instance = new static;
    }
    return self::$instance;
}

      

Otherwise, your constructor should just be private in order to be used again new self;

.

0


source







All Articles