views:

1525

answers:

3

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?

+4  A: 

Try to pass parameter class definition into the function:

function myFunction(Whatever $blah) {
}
maxnk
that works, but now I´ve expanded the question, hope you don't mind! Thanks for your great answer, that already helps.
Yar
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
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.
Yar
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
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
And now I'm in doubt about my favourite PHP IDE :(
maxnk
Yeah, check out source->format. Does pdt have that?
Yar
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
I'm doing my next project in Ruby, so we're even.
Yar
maxnk, check my new answer below now that you use Netbeans :)
Yar
A: 

I have found/invented the answer, but it's only for Netbeans.

  1. Make another PHP file in your project which you will not use (variableDefinitions.php, for instance).
  2. In the file from #1, include the variable you're interested in with it's type like this: $blah = new Account(); where Account is the type, of course.
  3. Now in any other file in the project -- even without including the new PHP you created in #1 -- you will get autocomplete for the $blah variable.
Yar
if my own answer could be marked best answer, this question would have a best answer. If anybody copies and pastes my answer as an answer, I will mark it best answer...
Yar
Ok, thanks, I'll use it :)
maxnk
+2  A: 

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

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

There is also inline phpDoc comment with do exactly the same thing.

public function myFunction($blah)
{
    /* @var $blah Whatever  */
    $blah-> 
    // Now $blah is Whatever object, autocompletion will work.
}
Alan Bem
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.
Yar
yes, but I'm using PDT 1.3
Alan Bem
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