tags:

views:

128

answers:

5

In my framework, I have a method that returns an array with objects. It looks pretty much like this:

    /**
     * @return array Array with Action objects
     */
    public function getActions() {
        return $this->actions;
    }

The user of this Class will get an doc-popup when calling this method, which only says that this method will return an array. But that's it! Next, when the user gets an object out of the array, the IDE is plain stupid and doesn't know / suggest / autocomplete anything.

Is there anything I can do as a framework developer to make life of the framework users easier here? Or what else can I suggest them to do, so their IDE knows what kind of object is returned from the array?

Or in general: How to let the IDE know what kind of object came from the array, so that it can suggest what methods are available for that object?

+1  A: 

php is a dynamically/weakly typed language, objects in arrays could be anything. so IDEs cannot know what object to expect. imagine the following array:

array ( 1, "string", someObject, array (1,"string",3) );
knittl
Well, it would still be conveivable to hint on certain members of the array (just as you can for objects). There is no phpDoc convention for that that I know of, though.
Pekka
+2  A: 

I think you can't hint on the type of an array member other than maybe assigning it explicitly like so:

$my_object = new myClass();
$my_array[567] = $my_object;

the only idea that comes to mind is to use an object for this->actions instead of an array. That object you could predefine for any decent IDE to understand.

Pekka
you mean like "faking" an selfmade array that returns specifically objects of type Class? Could do the trick, although pretty painful ;)
openfrog
I mean rather switching over to using an object instead of an altogether, if possible. It depends on what kind of stuff your array contains. But have you tried @hsz's answer? It looks pretty promising, if it works.
Pekka
+3  A: 

When you call getActions() method you can prepend comment line before iterated array of objects:

$actions = $o->getActions();

/* @var Action $action */
foreach ( $actions as $action ) {
    ...
}
hsz
+1  A: 

JetBrains new WebIDE has the best static code analysis, you can just use @return ElementType[] there. And it can infere types directly from the actual code too - see details on their site

Alexey Gopachenko
A: 

So you are saying, for this exemple, you just have to do at the end of the methode : @return Action[] ?

Your method :

/**
 * Description ...
 * @return Action[]
 */
 public function Action(){
     $actions = array();
     ...
     return $actions;
 }

The code :

 $actions = new Action();
 foreach($actions as $action){
     $action-> // here the auto-completion I'm looking for !
     ...
 }

That's all ? Because on my eclipse it doesn't work :( I'm working on Eclipse PDT, Any solution ?

malou