统一生成器接口
优质
小牛编辑
146浏览
2023-12-01
统一生成器接口
由于内部隐式rewind,需要先调用Generator::current
获取当前value,而直接调用Generator::send
会跳到第二次yield。
<?php
class Gen
{
public $isfirst = true;
public $generator;
public function __construct(\Generator $generator)
{
$this->generator = $generator;
}
public function valid()
{
return $this->generator->valid();
}
public function send($value = null)
{
if ($this->isfirst) {
$this->isfirst = false;
return $this->generator->current();
} else {
return $this->generator->send($value);
}
}
}