| ▲ | wesammikhail 6 hours ago |
| That's exactly it but PHP Core refuses to address the typed array issue in wait for a consensus on Generics even though those two things serve different audiences. I haven't actually run into a single issue with PHP's type system in the last few years. Literally the only thing that remains is typed arrays but that seems to be ways off due to fragmentation in how the core devs want it handled. |
|
| ▲ | hparadiz 5 hours ago | parent [-] |
| With how good PHPDoc and Phan are these days I don't really think about it anymore. You can sort of have typed array by implementing Iterator, Countable, ArrayAccess, and optionally Indexing but I assume you mean defining types of array values. |
| |
| ▲ | Tadpole9181 an hour ago | parent | next [-] | | If I could take a tangent and talk about the infuriating implicit-casting behavior of arrays in PHP? declare(strict_types=1);
/\* @var array<string, int> \*/
function foo(string $key) {
echo $key;
}
$foo = [
'a' => 10,
'1' => 15,
];
foreach ($foo as $key => $_) {
foo($key);
}
The static analyzer will say this is safe (sans a bleeding edge setting in PHPStan), but it will throw a type error when it runs. Because the string '1' is, despite having strict types enabled, still implicitly cast to the number 1. | | |
| ▲ | wesammikhail 4 minutes ago | parent [-] | | I had this happen to me yesterday in spite of knowing about it because I was rushing. I added phone numbers as array keys to deduplicate the whole set. Some ended up as ints and some as strings. PHP arrays are the most versatile part of the language. They're what actually makes the language so good in so many ways. But typed arrays are a freaking requirement at this point and I've been screaming about it for years now... alas no response. |
| |
| ▲ | someothherguyy an hour ago | parent | prev [-] | | pretty naive take, no run time guarantees, basically swinging on hope to keep your shit together. phpstan drops types on static analysis for deeply nested arrays/objects for example. or even those with many members. |
|