100

I have this code:

/**
 * Days to parse
 * @var int
 */
const DAYS_TO_PARSE = 10;
...

I don't think that using @var is correct for a constant and I don't see any @constant PHPDoc tag. What is the correct way to do this?

7
  • As far as define is concerned: stackoverflow.com/questions/2192751/… Commented Jul 15, 2011 at 11:08
  • I saw that one, define is for standalone constants, I am looking for a class constant Commented Jul 15, 2011 at 11:10
  • 1
    possible duplicate of phpDoc class constants documentation Commented Jul 15, 2011 at 11:10
  • @Elzo const FOO = 1; works outside class context as well. Commented Jul 15, 2011 at 11:13
  • this guy is using @access private icosaedro.it/phplint/phpdoc.html, but I am not aware of the fact that you can restrict visibility for constants Commented Jul 15, 2011 at 11:16

6 Answers 6

171

The PHP-FIG suggests using @var for constants.

7.22. @var

You may use the @var tag to document the "Type" of the following "Structural Elements":

  • Constants, both class and global scope
  • Properties
  • Variables, both global and local scope

Syntax

@var ["Type"] [element_name] [<description>]

Sign up to request clarification or add additional context in comments.

6 Comments

So what is essentially is short for "variable" we use for documenting something that is "constant"?
as of 2017 using @const will correctly output my description but @var will not output anything for a class constant.
This is outdated. The current version of the PSR-5 draft no longer specifically mentions this. I maintain that constants don't need a specific type hint because their type is immutable and can always be deduced: stackoverflow.com/a/50945077/752110
@Yogarine constants may not need a type-hint, but it may be desirable to document what the constant is used for
@BradKent Of course. In that case it suffices to just add a docblock without any annotations.
|
133

@const is not the right answer.

The only "official" place it's listed is phpdoc.de, but the spec there only ever made it to 1.0beta, and the site also includes tags like @brother and @sister, which I've never seen used before, so the overall trust in that site is somewhat diminished ;-) The de facto standard has always been phpDoc.org.

In short, even if some unofficial standard does mention it, if the documentation generators don't support it, then it's not worth using.

@var is correct for now, and once the PSR (last link in the above list) is out of draft, and is the basis for which phpDocumentor, Doxygen, APIGen and others are understanding PHPDoc, then @type would be correct which is the successor to @var.

2 Comments

In fact it doesn't seem to matter at all for IDEs, PHPStorm for example always takes the actual code value to find out the type (as it must have a value assigned).
8

There is no need to annotate the type of constants, since the type is always:

  • either a scalar or an array
  • known at declaration time
  • immutable

@const is also not part of the PHPDoc standard. PHP-FIG suggests @var but this is not backed by PHPDoc and doesn't add any information you can't already deduce from the declaration itself.

Therefore, for the sake of readability I recommend just using a plain PHPDoc docblock to document your constants:

class Foo
{
    /**
     * This is a constant.
     */
    const BAR = 'bar';
}

It will describe the constant when you generate PHPDocs yet keeps the comments clean and readable.

2 Comments

It's useful to declare the type of array elements
@Jacob Even then, the array would be constant, so any IDE / Static analysis tool should be able to simply infer the array shape.
4

I use Netbeans. It will parse phpDoc for global and class constants when this format is used:

/** @const Global constant description */
define('MY_CONST', 10);

class MyClass
{
    /** @const Class constant description */
    const MY_CONST = 10;
}

3 Comments

Can't you leave the @const out for the class constants in Netbeans?
I just tested in Netbeans 8, and was able to omit @const for the global and class constant declarations.
In my case, i added "/** Class constant description */" and it worked.
4

The following proposition respects the official documentation syntax:

class Foo
{
    const
        /**
         * @var string Should contain a description
         */
        MY_CONST1 = "1",
        /**
         * @var string Should contain a description
         */
        MY_CONST2 = "2";

}

Comments

-23

To get them into phpDoc, use:

@const THING

Usual construct:

@const[ant] label [description]

5 Comments

isn't the a difference between class constants and global constants initiated by define()? I guess @const is for notating the latter.
It's the former. I just documented a class constant and my generated phpdocs correctly contain the description. And as of April 2017 the English docs still don't have @const!
@const is not valid and does not exist in PHPDocumentor. Use @var.
It was valid at time of writing. Thanks.
Though Brian is correct that there seems to be evidence that at the time of writing, there was documentation on using a @const[ant] doc-style tag (PHP Doc 1.0 beta), I don't see why you wouldn't update an answer that is now incorrect after 11 years.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.