In other languages, like Java, you can use Collections that allows only one type. In TypeScript, for example we can do the following:
public getItems(): Array<Items>
{
return this.items;
}
Unfortunatelly, PHP doesn't have this kind of thing, that's why I created this abstract class
This package is installed via composer:
composer require maike/iterable
In order to achieve a Collection that allows only one type in PHP, we have to create a class. In the following example, I'm creating a Collection Class extending the Iterable\Iterator
abstract class
use Iterable\Iterator;
class DateTimeCollection extends Iterator {
public function __construct(\DateTime ...$items)
{
parent::__construct($items);
}
}
With the above code we achieved two things:
- We have a collection that only allows
DateTime
Objects. - We can iterate over our Collection as we do in an array. See the following example:
$datesCollection = new DateTimeCollection(
new \DateTime,
new \DateTime,
new \DateTime,
);
foreach ($datesCollection as $key => $date) {
// Do whatever you want
}
make build
make tests