在php 8.1 及以上版本中,直接运行上面的PDO代码会出现类似于:
Return type of TableRows::current() should either be compatible with RecursiveIteratorIterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in xxx
的报错,这是因为新版本的php在从标准库扩展类时,重载函数强制要求添加返回类型。将TableRows类作如下修改即可:
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current(): string {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}
function beginChildren(): void {
echo "<tr>";
}
function endChildren(): void {
echo "</tr>" . "\n";
}
}