Home >Backend Development >PHP Tutorial >How to Declare PHPDoc Type Hints for Arrays of Objects in PHP?

How to Declare PHPDoc Type Hints for Arrays of Objects in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-07 01:00:19347browse

How to Declare PHPDoc Type Hints for Arrays of Objects in PHP?

Declaring PHPDoc Type Hints for Arrays of Objects

When using PHPDoc to hint the type of member variables, you can specify @var followed by the desired type, such as @var SomeObj. However, you might encounter challenges when attempting to indicate that a member variable is an array of objects.

To address this, PhpStorm (IDE from JetBrains) provides a solution by allowing you to use / @var SomeObj[] / syntax. For instance:

/**
 * @return SomeObj[]
 */
function getSomeObjects() {...}

Alternatively, the phpdoc documentation suggests using the following approach:

/**
 * @return array<int>
 */
function getArrayOfIntegers() {...}

Here, the specified type, , indicates the expected type of each array element.

The above is the detailed content of How to Declare PHPDoc Type Hints for Arrays of Objects in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn