成都网站建设设计

将想法与焦点和您一起共享

PHP量级数据遍历方案:yield生成器

getMessage();
            exit();
        }
        static::$instance->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
    }

    public static function getInstance()
    {
        if (!(static::$instance instanceof \PDO)) {
            new static();
        }
        return static::$instance;
    }

    public static function excuteQuery(string $sql)
    {
        static::$sth = static::$instance->prepare($sql);
        static::$sth->execute();
    }

    //防止实例被克隆
    private function __clone()
    {
    }

    public static function cursor()
    {
        while ($row = static::$sth->fetch(\PDO::FETCH_ASSOC)) {
            yield $row;
        }
    }
}

DB::getInstance();
DB::excuteQuery("select * from bill_info");
foreach (DB::cursor() as $info) {
    echo $info['id'] . PHP_EOL;
}

当前题目:PHP量级数据遍历方案:yield生成器
当前链接:http://chengdu.cdxwcx.cn/article/jisjjg.html