Last active
September 3, 2023 23:40
-
-
Save Antnee/fb2c41b0e3cf65eadf0c to your computer and use it in GitHub Desktop.
PHP 7 Generator Return Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"); |
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!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ofbar
objects