统一生成器接口

优质
小牛编辑
141浏览
2023-12-01

统一生成器接口

由于内部隐式rewind,需要先调用Generator::current获取当前value,而直接调用Generator::send会跳到第二次yield。

  1. send方法参考
  2. 生成器参考
<?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);
        }
    }
}