Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

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

so i use:

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

does anyone have the same problem?

how do i work around it?

thanks!

like image 704
never_had_a_name Avatar asked May 09 '10 05:05

never_had_a_name


1 Answers

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 :


(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 :


(source: pascal-martin.fr)

like image 157
Pascal MARTIN Avatar answered Sep 22 '22 10:09

Pascal MARTIN