PHP Annotations Cheat Sheet
Aug 15, 2019
Foreword
Work in progress compiling a list of possible php annotations
@var
Used to describe a variable
Parameters:
typeexamples:int,string,bool,array,User,int|User,callable,mixedvariableexamples: $user, $iddescriptionexample: 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:
typeexamples:int,string,bool,array,User,int|User,callable,mixedvariableexamples: $user, $iddescriptionexample: 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:
typeexamples:int,string,bool,array,User,int|User,callable,mixedvariableexamples: $user, $iddescriptionexample: the id of a user
Examples:
/**
* @property string $name Selected name of the user
*/
class User
{
//
}