tags:

views:

294

answers:

3

I've got this constructor that takes an optional argument. The main problem with this is usability. The developer using my framework will catch a headache instantly because he doesn't know if he can provide an argument, what kind of argument, or if he can't at all. Conclusion: It just sucks. But PHPDoc may help a little bit if someone has a reasonable IDE like Netbeans installed ;)

So:

class ChildClass extends ParentClass {
    public function __construct() {
    $tplFile = func_get_arg(0);
    if (!isset($tpl)) {
        $tpl = 'index';
    }
    parent::__construct($tpl);
    }
}

How could I use PHPDoc here to indicate that there can be provided an optional [$tpl] argument?

+3  A: 

Declare a parameter and give it a preset:

public function __construct($my_argument = 0) 

my IDE (phpEd, which is phpDoc sensitive) interprets it correctly. phpDoc should, too, and put the parameter into braces:

show ([$my_argument])
Pekka
By declaring a single optional arg in the code itself, phpDoc and the reader of the raw code can both interpret that the method can be used without arguments.Whether you do this or not, just using the @param tag in the docblock will give phpDoc something to write about, and I know that Eclipse PDT at least will pick that up and show it when it pops up docs about the method being hovered over.
ashnazg
For more details on how @param can be used when you don't make arguments explicit in the function signature itself, see the bottom example at [1].... just imagine it with only the "@param mixed $v,..." line being used.[1] -- http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.param.pkg.html
ashnazg
A: 

You can use reflection class for this too.

Sarfraz
+1  A: 

I suspect this is outside of the capabilities of PHPDoc.

Let's face it - this is an example of the occasional hideousness of PHP and PHPDoc (being loosely based on JavaDoc) isn't designed to encapsulate this sort of freakish behaviour.

As such, I guess you'd just document it normally with a @param and add an sensible explanation there.

middaparka