Avoid using arrays! Arrays are very flexible, allow almost anything and can be combined in any way.
This makes them good, but also very annoying to use.
Collections are not a native concept of PHP, as there are no generics in PHP.
For this reason we are forced to use a workaround and if we want to achieve true type safety we must write boilerplate classes for each collection we want to use More...
With phpstan generic collections can be achieved, but in fact is not really type safe GenericCollection
use Comod\Ds\Collection\GenericCollection;
use Tests\Unit\Example\Author;
use Tests\Unit\Example\AuthorCollection;
/**
* @phpstan-var GenericCollection<Author> $authorCollection
*/
$authorCollection = new GenericCollection(
new Author(self::BAR),
new Author(self::BAR)
);
// $authorCollection->append('bullshit'); // phpstan/psalm detects invalid type
$author = $authorCollection->pop();
echo $author->getName(); // autocompletion works
foreach ($authorCollection as $author) {
echo $author->getName(); // autocompletion works as well
}
self::assertSame($authorCollection->count(), 1);
There is an Extension which can be used as an alternative for arrays
https://github.com/php-ds/ext-ds
https://github.com/php-ds/polyfill
https://www.php.net/manual/en/class.ds-queue.php
- polyfills can be used without the extension
- can be used with 8.2
- generic usage with phpstan, but polyfills are not complete phpstan-ready (annotations should look like this src/Ds/Queue.php see line 139)