-2

How do I enable code completion for clases and methods I create in code for in netbeans 8.

Thanks, Dion

dion
  • 47
  • 1
  • 5

1 Answers1

2

You really should google this kinda thing, but I'm in the mood to answer a question :-)

Anyway, most IDE's read your code in pretty much the same way. I can't attest to NetBeans specifically (I use PHPStorm), but the general idea is to make sure you add docblocks to your classes, methods etc. The IDE reads these and can then provide decent code-completion.

<?php

namespace App;

/**
 * Class MyClass
 * Does some stuff
 * @package App
 */
class MyClass extends SomeOtherClass
{
    /**
     * This is my var
     * @var string
     */
    public $myVar = 'some val';

    /**
     * This is my method
     * @param string $yourString
     * @return SomethingElse
     */
    public function myMethod ($yourString)
    {
        $this->myVar = $yourString;
        return new SomethingElse($this->myVar);
    }
}

Have a look at the PHPdoc site for the tag syntax. Most IDE's will also have a way of generating this for you as well.

Kevin Nagurski
  • 1,889
  • 11
  • 24
  • 1
    I find `@var` on class properties doesn't always work in NetBeans - so I add a getter with a `@return` and always use that to access it. – halfer May 29 '15 at 13:42
  • @halfer Good idea. As long as the PHPdoc is there to support the code and not the other way around. – Kevin Nagurski May 29 '15 at 13:43
  • ...Most IDE's will also have a way of generating this for you as well. You would think that this would be the default way of doing it. Scan the code I have written looking for keywords like 'class', 'function' etc and automatically allow me to select them in a dropdown in the appropriate places, why add extra code for this? – dion Jul 06 '15 at 09:44
  • @user2484775 the problem is that PHP is a loosely typed language, so it's easy to infer functions, classes and the like, but determining the types of arguments or returns can only really be achieved by (a) annotations or (b) actually running the code. Even with actually running the code, types can vary depending on data. – Kevin Nagurski Jul 06 '15 at 10:10