Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. <?php
  2.  
  3. class User {}
  4. class UserReduced {
  5. public function __construct(User $user) {
  6. $this->user = $user;
  7. }
  8. }
  9. class CustomUserCollection extends ArrayObject {
  10. public function __construct($input = [], $flags = 0, $iteratorClass = 'ArrayIterator') {
  11. // magic
  12. $input = array_map(function ($user) {
  13. return new UserReduced($user);
  14. }, $input);
  15.  
  16. parent::__construct($input, $flags, $iteratorClass);
  17. }
  18. }
  19.  
  20. $users = [];
  21. for ($i = 0; $i < 10; ++$i) {
  22. $users[] = new User();
  23. }
  24.  
  25. // Case
  26. $userReduces = iterator_to_array(new CustomUserCollection($users));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement