Search code examples
phpjsondocumentdataformat

What is the data type for JSON?


I am writing a php method according to phpDocumentor format. The method just returns json, and I wrote String at the momemnt. I am wondering is this correct?

/**
 * This is a method returns json
 * @return String
 */
public function _getJsonData() {
  .....
}

Solution

  • Yes ... but no.

    You are right that a function returning JSON is returning a string as far as PHP's type system is concerned, and that's all phpDocumentor cares about.

    However, by convention the built-in type is always lower-case, and a leading upper-case letter would indicate a class name, so you should be using @return string not @return String.

    Note also that unless you need your code to run on really old versions of PHP, you can specify the return type in the function signature itself:

    public function _getJsonData(): string {
      .....
    }
    

    At that point, just specifying @return string is fairly pointless, but adding a description of the string might be more useful:

    /**
     * @return string A string formatted in JSON ready for use with the frobulator
     */
    public function _getJsonData(): string {
      .....
    }