My Java experience tells me I can create generic collections like this:
Collection<E> collection = new Collection<E>();
I am wondering if this is possible in Laravel (at least 5.2+)? I have been using Laravel for nearly 3 years, and have just wondered about this now.
For example, I was thinking of this when creating a Laravel model transformer function as below:
Current Laravel function:
public function transformMultiple(Collection $models){
$collection = collect();
foreach($models as $m){
$collection->push($this->transform($m));
}
return $collection;
}
Hypothetical function using generics:
public function transformMultiple(Collection<Foo> $models){
$collection = collect();
foreach($models as $m){
$collection->push($this->transform($m));
}
return $collection;
}
With credit to a comment from @AlexSlipKnot, I believe PHP does not currently support Generics; however, it may support it in the future. His other link in the comments (a StackOverflow answer to a similar question) also provides in-depth guidelines to custom implementations; however, if one is to develop their own custom implementation, they need to beware of applicable O.O Design Principles.