8

When I define an object of a class using new like this

$blah = new Whatever();

I get autocomplete for $blah. But how do I do it when I have $blah as a function parameter? Without autocomplete I am incomplete.

Edit: How do I do it if it's in an include and PDT or Netbeans can't figure it out? Is there any way to declare types for variables in PHP?

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421

2 Answers2

21

Method in first comment is called "type hinting", but you should use that wisely. Better solution is phpDoc.

/**
 * Some description of function behaviour.
 *
 * @param Whatever $blah
 */
public function myFunction($blah)
{
    $blah-> 
    // Now $blah is Whatever object, autocompletion will work.
}

You can also use an inline phpDoc comment which does exactly the same thing.

public function myFunction($blah)
{
    /* @var $blah Whatever  */
    $blah-> 
    // Now $blah is Whatever object, autocompletion will work.
}
Alan Gabriel Bem
  • 536
  • 3
  • 11
  • Alan, are you saying this works in PDT because in Netbeans class Joe { public $a; public $b; } /* @var $joe Joe */ $joe-> doesn't give you autocomplete. – Dan Rosenstark Dec 28 '08 at 16:35
  • I just tried this in Netbeans 6.9.1 and it works like a charm. In my opinion simpler than the accepted answer, seems that the php support has improved since Yar's comment was added. – henrik Aug 11 '10 at 10:19
8

Try to pass parameter class definition into the function:

function myFunction(Whatever $blah) {
}
maxnk
  • 5,755
  • 2
  • 27
  • 20
  • that works, but now I´ve expanded the question, hope you don't mind! Thanks for your great answer, that already helps. – Dan Rosenstark Dec 23 '08 at 22:26
  • Ah, and about "Edit": I really don't know how to make it in elegant way. But my Eclipse+PDT installation resolve class parameters in similar cases well. – maxnk Dec 23 '08 at 22:28
  • Okay, if no one can beat that -- very possible, but maybe there's some commmenty way to do it -- I'll give you best answer. Try Netbeans, I just switched from PDT... you might dig it. – Dan Rosenstark Dec 23 '08 at 22:30
  • Thanks for advice, I'll definitely give Netbeans a try. To be fair - I didn't see the PHP in NB yet, just java and ruby. – maxnk Dec 23 '08 at 22:37
  • Hmm.. just tried this in NetBeans 6.5 and it works. I've made a Sample class with some vars, place it in separate file, include this file in my script, create the $sample var using new() and pass this var in function with type definition. And I've got the autocomplete for this var within function... – maxnk Dec 23 '08 at 22:53
  • And now I'm in doubt about my favourite PHP IDE :( – maxnk Dec 23 '08 at 23:00
  • Ok, you've won - I'll make my next project in NetBeans :) Also, take a look at this topic: http://www.netbeans.org/issues/show_bug.cgi?id=146248 - hope that they add something like /* @var variableName */ in NetBeans in future releases. – maxnk Dec 23 '08 at 23:42
  • I'm doing my next project in Ruby, so we're even. – Dan Rosenstark Dec 23 '08 at 23:45
  • maxnk, check my new answer below now that you use Netbeans :) – Dan Rosenstark Dec 24 '08 at 17:01