开发者

netbeans autocompletion when using singleton to retrieve object instead of new operator?

开发者 https://www.devze.com 2022-12-30 04:00 出处:网络
when i use the \'new\' operator to instantiate a class, netbeans has no problem to autocomplete the members of the object.

when i use the 'new' operator to instantiate a class, netbeans has no problem to autocomplete the members of the object.

$instance = new Singleton();
$instance-> // shows test() method

but when i use a singleton to retrieve an object it cannot autocomplete the members in the object retrieved.

the getInstance code looks like this:

public function test() {
    e开发者_运维技巧cho "hello";
}

public static function getInstance() {
if ( ! is_object(self::$_instance)) {
    self::$_instance = new self();
    self::$_instance->initialize();
}
return self::$_instance;
}

so i use:

$instance = Singleton::getInstance();
$instance-> // no autocompletion!

does anyone have the same problem?

how do i work around it?

thanks!


You could add a comment to indicate of which type $instance is, before assigning it :

/* @var $instance Singleton */
$instance = Singleton::getInstance();


And you'd get autocompletion :

netbeans autocompletion when using singleton to retrieve object instead of new operator?


(source: pascal-martin.fr)

(Tested with a recent nightly build of netbeans)



Another solution would be to add a docblock to the declaration of your getInstance() method, to indicate that it returns an instance of the Singleton class :

/**
 * @return Singleton
 */
public static function getInstance() {

}


And, then, you'll also get autocompletion :

netbeans autocompletion when using singleton to retrieve object instead of new operator?


(source: pascal-martin.fr)

0

精彩评论

暂无评论...
验证码 换一张
取 消