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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 使用singleton检索对象而不是new运算符时netbeans自动完成?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用’new’运算符来实例化一个类时,netbeans没有问题来自动完成对象的成员.
$instance = new Singleton();
$instance-> // shows test() method

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

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;
}

所以我使用:

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

有没有人有同样的问题?

我该如何解决这个问题?

谢谢!

在分配之前,您可以添加注释以指示$instance的类型:
/* @VAR $instance Singleton */
$instance = Singleton::getInstance();

你会得到自动完成:


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

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

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

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

}

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 使用singleton检索对象而不是new运算符时netbeans自动完成?全部内容,希望文章能够帮你解决php – 使用singleton检索对象而不是new运算符时netbeans自动完成?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。