-
-
Save Antnee/fb2c41b0e3cf65eadf0c to your computer and use it in GitHub Desktop.
<?php | |
class Item { | |
private $id; | |
public function __construct(int $id) | |
{ | |
$this->id = $id; | |
} | |
public function id() : int | |
{ | |
return $this->id; | |
} | |
} | |
class Collection { | |
private $items = []; | |
public function __construct(Item ...$items) | |
{ | |
$this->items = $items; | |
} | |
public function item() : Generator | |
{ | |
yield from $this->items; | |
} | |
} | |
$items = []; | |
for ($i=0; $i<10; $i++) { | |
$items[] = new Item($i); | |
} | |
$collection = new Collection(...$items); | |
$generator = $collection->item(); | |
printf("\nThe Collection::item() method returns a type of %s, as expected\n", get_class($generator)); | |
printf("However, it yields the following:\n\n"); | |
foreach ($generator as $item) { | |
printf("\tClass: %s. Calling id() method returns %d (%s)\n", get_class($item), $item->id(), gettype($item->id())); | |
} | |
printf("\n\nIt doesn't appear to be possible to hint the yielded type\n\n"); |
Probably. I do know that some people are pushing for generics. That should also bring the ability to create an "array of types" as well. Hopefully something like $foo = []bar;
to create an array of bar
objects
Here is working solution, where $items = []Item
: https://3v4l.org/OH18b
Or better one: https://3v4l.org/YMQer
@gallna - It's not the prettiest or most obvious piece of code I've ever seen, but I like that last solution. It does what I need it to :) It's a shame that it's such a PITA to get it to do what we're trying to do, but it certainly does it here. Nice one!
hey @gallna - do you have another link for https://3v4l.org/YMQer by chance? That one produces a 500 and I am interested in your solution =) Thank you!
It works for me @rhift
<?php
class Item {
private $id;
public function __construct(int $id)
{
$this->id = $id;
}
public function id() : int
{
return $this->id;
}
}
class Collection extends IteratorIterator
{
public function __construct(Item ...$items)
{
parent::__construct(
(function() use ($items) {
yield from $items;
})()
);
}
public function current() : Item
{
return parent::current();
}
}
$items = [];
for ($i=0; $i<10; $i++) {
$items[] = new Item($i);
}
$collection = new Collection(...$items);
foreach ($collection as $item) {
printf("\tClass: %s. Calling id() method returns %d (%s)\n", get_class($item), $item->id(), gettype($item->id()));
}
oh weird, thanks for pasting it @Antnee!
Sorry, I see the point now. I think we'll wait long to see implementation you expect.