AndrewRMillar
AndrewRMillar

Reputation: 662

What is the difference between curly braces and angle brackets for type hinting in PHP docblock?

I see both angle brackets and curly braces used in PHP docblocks used when typehinting arrays.

/**
 * @return array<name:int,link:string,items:array<class:string,active:bool>>
 */

Or

/**
 *@return array{name:string,active:bool}
 */

I've tried to find something on this type of type hinting in docblocks in the documentation, but found nothing.

Can anyone clear this up for me.

Upvotes: 2

Views: 647

Answers (1)

AndrewRMillar
AndrewRMillar

Reputation: 662

So after some consulting with a colleague, he explained it roughly as follows:

When you are type hinting an array in a php docblock you use the angle brackets <> to indicate types at different indices in the array.

You use curly braces {} for associative arrays where you specify the keys and types their values have.

/**
 * Only angle brackets:
 * @return array<int,string,bool>
 */

Vs.

/**
 * A combination of curly braces and angle brackets
 * @return array{name:string,active:bool,items:array<int,string,bool>}
 */

For anyone who had the same question

Upvotes: 4

Related Questions