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?

2 Answers 2

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.
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
8

Try to pass parameter class definition into the function:

function myFunction(Whatever $blah) {
}

9 Comments

that works, but now I´ve expanded the question, hope you don't mind! Thanks for your great answer, that already helps.
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.
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.
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.
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...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.