

If you annotate some parameter with a tag, PhpStorm will provide you with the appropriate type inference:

With such code, it is common to pass a class name string as an argument and receive an object as a result.

Nested generics like Base> are not supported, and will likely not be supported in the future, because of the performance application of generics is containers and factories. For such annotations, PhpStorm will understand the T types. You can only get type inference and completion for first-level generics in PhpStorm, i.e. With such annotation, you’ll be able to get code completion in a foreach loop: So a workaround for this could be using a specific collection in the type hints, for example, Arra圜ollection. Because to support this, two levels of template passing should be implemented: Doctrine Collection => IteratorAggregate. Iterating over the Doctrine Collection interface does not currently work. With the Doctrine Collections, you will already get a code-completion in simple scenarios: Here is a simple example of the annotated collection class: In short, you get more bugproof code and better code completion. Check out the Generics and why we need them post by Brent Roose to learn more about the advantages of such an approach over arrays. In PhpStorm 2021.2, you’ll be able to use generic class collections. What will work in PhpStorm 2021.2?Ĭollections with of the most popular applications of generics is collections. Read more about PhpStorm’s support for Psalm and PHPStan. This support came with a basic “mirror” scenario for generics with the annotation. We introduced support for Psalm and PHPStan in PhpStorm 2020.3. So there is really no wonder why the ` WI-47158 Generics support with ticket is one of the most upvoted in the PhpStorm issue tracker. Their adoption has grown quickly and many popular open-source PHP projects now rely on the generics annotations. On the bright side, generics have been introduced in static analysis tools such as Psalm and PHPStan.

However, the conclusion he came to is that all the various possible ways of implementing them have serious problems. Nikita Popov researched the feasibility of this in PHP and even prototyped a PoC. Although generics is one of the most wanted features in the PHP language, according to the JetBrains D eveloper Ecosystem survey, there are no plans to add them any time in the near future. Support for Generics Are there generics in PHP? We are adding preliminary support for generics in PHP.
