Grayscale profile picture

Patrique Ouimet

Developer

COVID-19 Stats

PHP Annotations Cheat Sheet

Thu, Aug 15, 2019 8:33 AM

Foreword

Work in progress compiling a list of possible php annotations

@var

Used to describe a variable

Parameters:

  • type examples: int, string, bool, array, User, int|User, callable, mixed
  • variable examples: $user, $id
  • description example: the id of a user

Examples:

class User
{
    /** @var string */
    protected $name;

    /**
     * @param string $name the name of a user
     */
    public function __construct(string $name)
    {
        $this->name = $name;
    }

    /**
     * @return Post|null
     */
    public function getLatestPost() : ?Post
    {
        /** @var Post $post */
        $post = Post::where('user_id', $this->id)->latest()->first();

        return $post;
    }
}

@param

Used to describe a function or method argument

Parameters:

  • type examples: int, string, bool, array, User, int|User, callable, mixed
  • variable examples: $user, $id
  • description example: the id of a user

Examples:

/**
 * @param int $id the id of a user
 * @return User|null
 */
public static function getUserById(int $id) : ?User
{
    return User::find($id);
}

@property

Used to describe a class property

Parameters:

  • type examples: int, string, bool, array, User, int|User, callable, mixed
  • variable examples: $user, $id
  • description example: the id of a user

Examples:

/**
 * @property string $name Selected name of the user
 */
class User
{
    //
}

@property-read

@method

@return

@throws

@final

@author

@see

@experimental

{@inheritdoc}

@internal

@PhpUnused

@package

@deprecated

@suppress

@override