189 8069 5689

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://cdxtjz.cn/article/jisjjg.html

其他资讯