Search code examples
phpcodeigniter

What is the Reason to Write @param Types in All Caps, in PHP/Codeigniter?


I am using php version 7.4.3, Codeigniter version 3.1.13, and VSCode for my IDE.

When I write functions I will add some notes and define the parameters, and return outputs like so. enter image description here

I noticed that when I capitalize the parameter and output types, my IDE highlights them in a nice green color, as if that was what I was supposed to do all along.

enter image description here

Should I be capitalizing the the @param and @return datatypes, and what is the reason for doing so?


Solution

  • You should avoid using capitalized STRING for PHP doc block, because it would mean that string is a class, but it is a built-in scalar type.

    VSCode shows different colors in doc block for built in types and class-types (class or interface or enum or trait).

    If you use the capital STRING, your IDE may be confused.

    I strongly recommend to use the lower string version like in your first picture. Of course it depends on your and your team's coding style, but one of the most popular PHP standard (PSR-12) says:

    All PHP reserved keywords and types [1][2] MUST be in lower case.

    Anyways, PHP allow using case insensitive names, but for readability it should be lower case. And all user defined class-types will be the "nice green color". Of course if you change the VSCode theme the colors may change.

    You can play with the types and check them in VSCode.

    /**
     * @return object|resource|array|string|float|double|int|bool|false|true|null|mixed|void|self|static|callable|never|iterable|\Traversable|\Closure|\stdClass|\Error|\Iterator
     */
    function test()
    {
    }