Search code examples
phpphpdoc

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?


Solution

  • 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.