r/PHPhelp • u/notkingkero • Aug 22 '25
Difference between array, array<mixed> and mixed[]?
In my head, array, array<mixed> and mixed[] represents the same thing.
However, there seems to be a difference between array|\Foo[], array<mixed>|\Foo[] and mixed[]|\Foo[] (see here in PHPStan playground). Is my original assumption wrong or the type detection buggy?
3
Upvotes
11
u/eurosat7 Aug 22 '25
It doesn't matter as all three are worthlessly unspecific.
1
u/Radiant-Somewhere-97 Aug 22 '25 edited Aug 22 '25
I may be wrong, but
string[] != array<string>string[] = list<string>array<string> - may be indexed by anythinglist<string> and string[] - indexed by 0, 1, 2, 3, ...1
u/universalpsykopath Aug 22 '25
mixed[] array of anything.
array<mixed> an array where the values can be anything and the keys are arbitrary.
array<int|string,mixed> an array where the values can be anything but the keys are either integers or strings.
array<TKey, Tvalue> A template array for use in PHP Stan generics. This is a cool feature of Stan.
So you could provide a basic collection with a template of <int, TValue> and a constructor with $items <int, TValue> TValue can be anything, but anywhere you try to mutate TValue into another type, Stan will throw an error.
So whatever TValue is defined as, it must be consistent across the implementation. This is extremely useful for custom iterators.
1
u/MartinMystikJonas Aug 23 '25
PHPStan uses concept of explicit and implicit mixed that is reason probably
0
u/cursingcucumber Aug 22 '25
array|\Foo[]- untyped (mixed) array vs typed listarray<mixed>|\Foo[]- mixed array vs typed listmixed[]|\Foo[]- mixed array vs typed listOf course they are different? 😶
And as mentioned,
Something[]is a list (numerically indexed), as islist<Something>. An array is either associative or numerically indexed.0
u/Plastonick Aug 22 '25
I'm not aware of any differences in how
array,array<mixed>andmixed[]are interpreted. None of them are interpreted strictly as list types.I suspect PHPStan may be interpreting the union types incorrectly personally, seems like a bug to me.