PHP autocomplete in Netbeans

In Netbeans PHP autocomplete does not always work because the type of a variable is not clear. Below you can read how to hint the type thus allowing for autocomplete to work in Netbeans with PHP as well as it does with Java.

Code completion is one of the big upsides of developing code in an IDE so it is quite frustrating if it does not work properly. For example, in the following PHP function, the type of the object variable is unknown to Netbeans, so auto complete does not work:

public function my_function($object) {
    $object-> // php autocomplete does not work.
}


Getting Auto-Complete to work for PHP method parameter in Netbeans

Since PHP 5 type hinting in the method signature is allowed:

public function my_function(MyClass $object) {
    $object-> // php autocomplete works!
}
Using type hinting autocomplete will work in Netbeans. But what if you do not want do change the behavior of the PHP code at all? After all, declaring the type in the method signature leads to a fatal error if an object of a different type is passed (although your code really should not allow that to happen – and if it does, failing might be a good thing). It is also possible to hint to the type in the PHPdoc comment in Netbeans:
/**
 * @param MyClass $object
 */

public function my_function($object) {
    $object-> // php autocomplete works!
}

Getting Auto-Complete to work for all variables in Netbeans

The above solutions are clean and work well for method parameters. But what if the variable is not a parameter? It is possible to use a comment anywhere in the code and declare the type of a variable there to enable code completion:

public function my_function() {
    /* @var $object MyClass  */
    $object = get_object();        
    $object-> // php autocomplete works!
}
This can also be used directly inside a loop:
public function my_function() {
    foreach($objects as /* @var $object MyClass */ $object) {
        $object-> // php auto-complete works!
    }
}
And there you have it: Netbeans auto complete works as well with PHP objects as it does with Java. I prefer hinting to the type directly in the method signature where possible but choose whichever one you like best.

Related Posts:

3 thoughts on “PHP autocomplete in Netbeans

  1. it looks great! but I canĀ“t get it to work….

    example. I extend PDO class in Database Class.
    $db = new Database.
    $db->….. [no hinting....] have to know all methods… or properties…

    examples…
    $db->prepare();
    $db->execute():

    spent lot of time looking for … setFetchMethod()
    $db->setFetchMode(PDO::FETCH_ASSOC);

    how can I get this autocomplete?? hinting??

  2. Hey Daniel,

    actually, in your case autocomplete should work without type hinting because you instantiate the Database class explicitly. Type hinting should only be necessary if the type of the variable is not clear in the current context.

    Do you get any options for autocomplete? If only setFetchMode is not shown it might be because PDO does not have that method, it is a method of PDOStatement.

    $db = new MyPDO();  // MyPDO extends PDO
    $db = $db->prepare();  // prepare returns a PDOStatement instance
    $db->setFetchMode($mode) // this was auto-completed
  3. Type hinting doesn’t work when you want to hint only the parent class so child classes can autocomplete. What is the point of parent classes in PHP when the child can’t access parent objects with auto complete?

    This is infuriating, I’ve been 4 hours trying to find a solution for this

Leave a Reply

Your email address will not be published.