Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

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 type hinting in docblocks in de docblock documentation, but have not been able to find anything.

Can anyone clear this up for me.

question from:https://stackoverflow.com/questions/65951133/what-is-the-difference-between-curly-braces-and-angle-brackets-for-type-hinting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...