Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark that an argument is optional in PHPDoc?

Tags:

oop

php

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?

like image 273
openfrog Avatar asked Dec 31 '09 12:12

openfrog


1 Answers

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])
like image 166
Pekka Avatar answered Nov 12 '22 08:11

Pekka