Most likely the task implies using this: https://ru.wikipedia.org/wiki/Collection_(programming)
You can implement your collection on the interface: http://php.net/manual/ru/class.ds-collection.php
Collection This interface is intended to describe the basic functions of working with a variety of objects. It inherits the Countable and Iterable interfaces, which allows you to get the number of objects in a collection and to crawl and apply a custom function for each collection object. The collection interface implies that the collection contains objects of the same type.
In your case, the most suitable implementation of the collection sets .
Implementations of sets Sets are represented by the only implementation of UniqueStore. Objects in the store UniqueStore. The uniqueness of the objects is provided by the getIdentity () method, which returns object identifiers. Multiple objects with the same identifier cannot be present in the UniqueStore. The internal structure of the storage of unique objects UniqueStore is built on the basis of associative links between objects and their identifiers. This makes it possible to implement all storage operations using associative samples, which greatly increases its speed. The complexity of the unique object storage algorithms is O (1), which means that the installation / retrieval time of the objects does not change depending on the size of the storage. The storage of unique objects UniqueStore supports any data types for values.
Examples of using the kit:
namespace Rmk\Collection; use \UnexpectedValueException as UnexpectedValueException; use \InvalidArgumentException as InvalidArgumentException; use \stdClass as stdClass; include '../../bootstrap.php'; $set = new UniqueStore('stdClass'); $obj1 = new stdClass(); $obj2 = new stdClass(); $obj3 = new stdClass(); // Добавление объектов в хранилище. $set->add($obj1); $set->add($obj2); $set->add($obj3); // Повторно объекты в хранилище добавлены не будут. $set->add($obj3); try { $set->add(new UnexpectedValueException); } catch (InvalidArgumentException $exc) { echo 'Значение не подходит по типу.'; } // Обход хранилища. $set->each(function($value, $thisSet) { /** * @TODO: Обработка хранилища. */ } ); // Удаление объектов из хранилища. $set->remove($obj1); $set->remove($obj2); $set->remove($obj3); // Преобразование в массив. $array = $set->toArray();
Source: https://habrahabr.ru/post/144182/