views:

37

answers:

3

Hello,

Let's say i have a function that returns an object of type SomeClass. And i have code like this:

$test = function_to_return_someclass();

Now i want to use the $test variable in an IDE, but i want it to understand that $test is of type SomeClass. I can do it easily with class variables by using the /** @var */ comment, but this is where i get stuck. And since trying something like:

$test = (SomeClass)function_to_return_someclass();

doesn't work, how can i instruct the IDE that $test is a SomeClass' object?

+3  A: 

You could try using @return in the function definition:

/** 
  * Generates an object of the class SomeClass
  * @return SomeClass the class 
  */
function_to_return_someclass()
 {
   ....
 }

it's up to your IDE whether it's smart enough to understand it. It should, though.

2nd approach: Try

 /** 
  * My object. Recognize it already, damn IDE!
  * @var SomeClass 
  */
 $test = function_to_return_someclass();
Pekka
I have @return in all my methods. Somehow it doesn't understand in some cases.
Marius
@Marius I see, I've had similar problems. I have added a second attempt. Why do you need to load classes this way? Maybe using an autoloader is an option?
Pekka
What annotation is @desc? Is that IDE specific? It's not in the PHPDocumentor manual. @var is only used when declaring class variables. And there should not be a line between the docblock and the function signature
Gordon
Sorry, it does work! I've had an uppercase letter in the @return's type where it shouldv'e been lowercase. Php didn't find it weird, but the IDE did. Good thing i noticed.And i don't load classes this way. This is a case where i use lots of objects of the same class. So for every object there is a convenience method to create and prepare it.
Marius
@Gordon cheers! Fixed. `@desc` must indeed be specific to something I've used at some point, can't remember what right now - not relevant. `@var` should be fine in the context used, though, shouldn't it? Seeing as I'm assigning a variable?
Pekka
@Pekka well, it says class variables, but I won't fuss about it :)
Gordon
@Gordon aww, good point! Could be that this makes trouble when creating documentation. I'd say it's okay if it fixes the OP's IDE problem, but that seems to have gone away already.
Pekka
+1  A: 

Some IDEs may be able to infer $test's type from the function called to retrieve it. From personal experience, Aptana Studio's old PHP plugin (by old, I mean 'Aptana 1.5' old) does that.

If you document the function itself:

/**
 * @return SomeClass
 */
function function_to_return_someclass()
{
    return new SomeClass();
}

And then call it, the IDE's autocomplete/IntelliSense should be able to list SomeClass's members.

BoltClock
A: 

You could try:

/**
* @return ClassToBeReturned
*/
function_to_return_someclass() {}
smoove666