简体   繁体   中英

PHPDoc - Different objects with different class within an array

Let's say that I have a function that returns array $data with a User object $user and a Car object $car

function my_function(){
    return array(
        new User(),
        new Car()
    );
}

What should I use for my @return parameter for the function documentation?

I Would do it this way

/**
 * @return User[]|Car[]
 */

General | means or and SomeType[] means there is an array of SomeType

Are you Sure, you did not want this?

/**
 * My Parent Object
 */
class SomeClass {

    /**
     * The User
     * @var User
     */
    public $User;

    /**
     * The Car
     * @var Car
     */
    public $Car;
}

The good (evil) thing about PHP is, you can mix array types. In a strong Typed Language (for reason) you can not. Here we use Interfaces and something like this. What we try with PHPDoc is to strong type PHP. So you should play the rules of a strong typed language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM