使用singleton检索对象而不是new运算符时netbeans自动完成?

nev*_*ame 6 php netbeans

当我使用'new'运算符来实例化一个类时,netbeans没有问题来自动完成对象的成员.

$instance = new Singleton();
$instance-> // shows test() method
Run Code Online (Sandbox Code Playgroud)

但是当我使用单例来检索对象时,它无法自动完成检索对象中的成员.

getInstance代码如下所示:

public function test() {
    echo "hello";
}

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

所以我使用:

$instance = Singleton::getInstance();
$instance-> // no autocompletion!
Run Code Online (Sandbox Code Playgroud)

有没有人有同样的问题?

我该如何解决这个问题?

谢谢!

Pas*_*TIN 12

$instance在分配之前,您可以添加注释以指示哪种类型:

/* @var $instance Singleton */
$instance = Singleton::getInstance();
Run Code Online (Sandbox Code Playgroud)


你会得到自动完成:

http://extern.pascal-martin.fr/so/question-2796730-netbeans.png

(最近每晚建立的netbeans测试)



另一个解决方案是将docblock添加到getInstance()方法的声明中,以指示它返回Singleton类的实例:

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

}
Run Code Online (Sandbox Code Playgroud)


然后,您还将获得自动完成功能:

http://extern.pascal-martin.fr/so/question-2796730-netbeans-2.png