Method returns an instance of the class

Hi,

The framework I'm using contains an abstract implementation for singletons (attached here) called `SingletonFactory`. When calling the method `getInstance` on any child member an instance of the class (not the `SingletonFactory`-class) is returned.

class ABC extends SingletonFactory {
     public function bla() {
          // do something
     }
}

ABC::getInstance()->bla();


Unfortunately PhpStorm assumes (based on the PhpDoc) that an instance of `SingletonFactory` is returned. Is there any way to fix this? (Possible without changing the class itself?)

Thanks,
Magnus



Attachment(s):
SingletonFactory.class.php.zip
2 comments
Comment actions Permalink

Hi there,

Please use @return static instead (in original SingletonFactory class). @return $this should also work.

Without modifying original code of your framework -- only this way, unfortunately -- via intermediate variable:

/** @var ABC $b */
$b = ABC::getInstance();
$b->bla();



P.S.

    /**
     * list of singletons
     * @var    array<SingletonFactory>
     */
    protected static $__singletonObjects = array();


Correct way of declaring such typed array is:

     * @var    SingletonFactory[]
0
Comment actions Permalink

HI,

Thank you for your response. It's not mine framework and therefor not my fault ;).
Adding "@return static" to the class works perfectly together with telling git to ignore the file change. (git update-index --assume-unchanged)

Magnus

0

Please sign in to leave a comment.