Search code examples
phpdocumentationphpdoc

Documentation: how do I document that a function can return more than one type of variable?


Assume a PHP function like this:

/**
 * Description
 *
 * @return  Namespace\GenericObject
 */
function doSomething() {
    if ($some_parameter) {
        return new GenericObject;
    } else {
        return FALSE;
    }
}

How should the return of the function be documented, using PHPDoc standard? As far as I know, a function should only have one return documented, yet, the function above can return either a new object, or a bool.


Solution

  • Use the or | operator.

    /**
     * Description
     *
     * @return  Namespace\GenericObject|null|array|false
     */
    function doSomething() {
        if ($some_parameter) {
            return new GenericObject;
        } else {
            return FALSE;
        }
    }