当我使用’new’运算符来实例化一个类时,netbeans没有问题来自动完成对象的成员.
$instance = new Singleton(); $instance-> // shows test() method@H_301_2@但是当我使用单例来检索对象时,它无法自动完成检索对象中的成员.
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; }@H_301_2@所以我使用:
$instance = Singleton::getInstance(); $instance-> // no autocompletion!@H_301_2@有没有人有同样的问题?
我该如何解决这个问题?
谢谢!
在分配之前,您可以添加注释以指示$instance的类型:
/* @var $instance Singleton */ $instance = Singleton::getInstance();@H_301_2@你会得到自动完成:
http://extern.pascal-martin.fr/so/question-2796730-netbeans.png
(最近每晚建立的netbeans测试)
另一种解决方案是在getInstance()方法的声明中添加一个docblock,以指示它返回Singleton类的实例:
/** * @return Singleton */ public static function getInstance() { }@H_301_2@http://extern.pascal-martin.fr/so/question-2796730-netbeans-2.png