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
Release: 2024-12-07 01:00:19
Original
216 people have browsed it

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() {...}
Copy after login

Alternatively, the phpdoc documentation suggests using the following approach:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template