A collection is a data structure that can hold number of objects (elements). It provides services for adding and removing elements. To be able to traverse all objects in collection the client can request an Iterator.
By definition collection contains unique objects, i.e. you cannot add an object twice.
Here we will define an abstract Collection interface that can be implemented from other classes. Unfortunately the PHP 4 version doesn't provide native interface, so we have to use inheritance.
/** * PHP4 * * Define collection interface. * * @author Ivan Georgiev * @abstract */ class ig_Collection { /** * Adds an element (reference) to this collection. * If element already exists, it will not be added. * * @param Object $obj Element to add to collection * @return boolean Retruns true if element was added. * @abstract */ function add(&$obj) { } /** * Remove an element (reference) from this collection. * If element doesn't exist, it will not be removed. * * @param Object $obj Element to be removed. * @return boolean Retruns true if element was removed. * @abstract */ function remove(&$obj) { } /** * Creates an iterator over the elements in collection. * @return ig_Iterator Returns a reference to an iterator. * @abstract */ function & iterator() { } }
Known implementations: Array Collection