openfrog
openfrog

Reputation: 40765

How to mark that an argument is optional in PHPDoc?

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?

Upvotes: 5

Views: 3698

Answers (2)

Pekka
Pekka

Reputation: 449803

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])

Upvotes: 6

Sarfraz
Sarfraz

Reputation: 382909

You can use reflection class for this too.

Upvotes: -1

Related Questions