Community
Participate
Working Groups
Build ID: M20070212-1330 Steps To Reproduce: create this: class A{ var $a; } class B{ var $b; } class Factory{ /** * @param string $name * @return A|B some description here.. */ function get($name){ // some code here.. } } $factory = new Factory(); $a = $factory->get("A"); $a-> <-- should get autocompletition hint for class A or at least for all return types More information: of course if we change * @return A|B to * @return A or * @return B everything works fine.. I think that multiple types should work because it is defined in standard (http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.return.pkg.html)
I've found similar problem here: http://209.85.129.104/search?q=cache:zd8rSEJ0I1UJ:framework.zend.com/issues/browse/ZF-1221+phpdoc+pdt+return+multiple+type+%40return&hl=pl&ct=clnk&cd=1&gl=pl&client=firefox-a but using /** * @return mixed A or B; some descrip here.. */ is not a solution
I think a good solution would be somekind of comment typehinting for when the objects are returned. for example: $factory = new Factory(); //@hint Class_Name $object = $factory->get("Class_Name");
(In reply to comment #2) > I think a good solution would be somekind of comment typehinting for when the > objects are returned. > > for example: > > $factory = new Factory(); > //@hint Class_Name > $object = $factory->get("Class_Name"); > You can already do this. Example: $factory = new Factory(); $object = $factory->get("ClassName"); /* @var ClassName $object */ But this doesn't fix the main problem with not being able to specify multiple return types. If anything, the editor should just recognize the first one. A lot of times, people make a method return an object or false (or null). As noted in the second comment, there are several methods in the Zend Framework whose @return doc tag has multiple values.
(In reply to comment #3) > /* @var ClassName $object */ My mistake. That should be: /* @var $object ClassName */
> $a = $factory->get("A"); > $a-> <-- should get autocompletition hint for class A or at least for all > return types ok, now pretend $sClassNameToGet was initialized from a command line parameter or via an xml config file: $a = $factory->get($sClassNameToGet); $a-> <-- what should appear in an auto-complete box here? A better solution for the coder might to use interfaces and then set the @return of the the factory's get() method to @return myInterface Obviously this solution is not appropriate for some situations where you're returning different classes that don't support the same interfaces. e.g. a bean factory. that's where the type hinting (see comment #4) should be used. this is what I do and it's ok, but this is part of the problem with having a non-typed language. in type'd languages you'd have to do something like this anyway: Object = <ClassA>Factory->get('A'); so in terms of code readability: $object = $factory->get('A'); /* @var $object ClassA */ it's not too bad, but then it is php :D Always remember the fact that incorrect documentation is often worse than no documentation, if the editor's telling you you've got loads of methods available which are not suited to BOTH classes (in the case of using @return with multiple return types) then you might write some code which uses a non existent method without realizing, only to find out when you run the code and it dies with a fatal error.
Fixed in CVS. Both ways are usable: 1. /** * @return A|B */ function foo() { } foo()->| <--- list members of A and B 2. /* @var $v A */ $v->| <--- list members of A
Tested on 2.2.1SR1. Closing.