PHP Collections
As everybody knows PHP is OOP language but not in the same way as other languages (ex. Java). So sometimes we can find there some gaps. One of them is a lack of good solution for collections. As we are working in OOP (or even something which is extension of it) we want to have control on types of objects we are using. PHP is not good on type hinting at all or in the other hand is the best one because is so flexible. We can only force arguments to be arrays, Objects of Classes (or Interface) and callable. But if we want array of specific type it’s impossible to force.
We can solve this issue in few ways, first of all will be every time we want to use method form assumed object we should check it by is_a() function. Or instead of passing that as array of objects we can add it one by one using method defined like below to be sure of getting expected type.
<?php
public function add(MyClass $object)
{
$this->data[] = $object;
}
Available Solutions
There are some solutions available, but none of them are not focused on type hinting and some not even implement Iterator.
Proposed Solution
In other languages we can find solutions for our issue or we can say there is no need for solution because there is no issue. Like Generics in Java or Hack Collections. Proposed solution is based on all good practices about collection adopted to a PHP world. It’s not an ideal solution as this is a kind of trick to give us at least some of collections flavor in PHP.
You can find it at wnowicki/collections. Feel free to contribute.
- Category:
- Development