问题描述
当我使用'new'运算符实例化一个类时,netbeans可以自动完成对象的成员.
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.
getInstance代码如下:
the getInstance code looks like this:
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
之前,您可以添加注释以指示其类型:
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测试)
另一种解决方案是在getInstance()
方法的声明中添加一个文档块,以指示它返回Singleton
类的实例:
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自动完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!