Actually, there is a task to create an object from the nested collections of tickets objects. But what is it?

Here is an object with an attached tickets object:

stdClass Object ( [email] => asd@asd.ru [phone] => +79243000155 [tickets] => stdClass Object ( [ticketType] => Adult [person] => Array ( [fullName] => Хабраков Иван Иванович [FirstName] => Иван ) ) ) 

tickets must be presented as a collection of objects . And how should it look and, accordingly, be created?

    2 answers 2

    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/

    • But ds collection is a plugin extension. And it requires php seventh version? And what about 5x? - Zhi V
    • @ZhiV is a valid implementation like this: habrahabr.ru/post/64840 but, here is the implementation of the "List" collection. However, the only difference with the "set" is that using the "list", if you make a mistake in the logic of the script, then you have the opportunity to create duplicate tickets in the "list". - ragmon

    Maybe I will be wrong, and I hurry somewhere, but why not make a collection like this?

    I do not deny that there are few methods here, you can still add a ticket search and something else is possible, but the task is to set the vector of thinking.

     <?php Class Ticket { protected $ticketType; protected $person; public function __construct($ticketType,$person) { $this->ticketType = $ticketType; $this->person = $person; } } Class Collection implements Iterator, Countable { private $tickets = array(); private $position; public function __construct(){ $this->position = 0; } //Добавить элемент в массив public function push($ticket) { if($ticket instanceof Ticket === false) { throw new InvalidArgumentException('Попытка добавить не верный тип'); } array_push($this->tickets,$ticket); } //Взять последний добавленный public function pop() { array_prop($this->tickets,$ticket); } //Ниже Методы для реализации интерфейса Iterator function rewind() { $this->position = 0; } function current() { return $this->tickets[$this->position]; } function key() { return $this->position; } function next() { ++$this->position; } function valid() { return isset($this->tickets[$this->position]); } function count(){ return count($this->tickets); } } $ticketJhon = new Ticket('emptyTicket',"John"); $ticketJane = new Ticket('emptyTicket',"Jane"); $collection = new Collection(); $collection->push($ticketJhon); $collection->push($ticketJane); foreach($collection as $ticket) { print_r($ticket); } $ticketStr = 'Не билет'; $collection->push($ticketStr);