derived-collections.rst 738 B

1234567891011121314151617181920212223242526
  1. Derived Collections
  2. ===================
  3. You can create custom collection classes by extending the
  4. ``Doctrine\Common\Collections\ArrayCollection`` class. If the
  5. ``__construct`` semantics are different from the default ``ArrayCollection``
  6. you can override the ``createFrom`` method:
  7. .. code-block:: php
  8. final class DerivedArrayCollection extends ArrayCollection
  9. {
  10. /** @var \stdClass */
  11. private $foo;
  12. public function __construct(\stdClass $foo, array $elements = [])
  13. {
  14. $this->foo = $foo;
  15. parent::__construct($elements);
  16. }
  17. protected function createFrom(array $elements) : self
  18. {
  19. return new static($this->foo, $elements);
  20. }
  21. }