我想扫描目录和子目录,列出 xml 文件,从 xml 文件中获取内容并显示它。此功能在没有 OOP 的情况下正常工作。我尝试创建一个类。我scandir_through
从函数调用函数main
。我没有错误,结果也是。
class data {
var $dir = 'D:\wamp4\www\begin';
public function scandir_through($dir)
{
$items = glob($dir . '/*');
for ($i = 0; $i < count($items); $i++) {
if (is_dir($items[$i])) {
$add = glob($items[$i] . '/*');
$items = array_merge($items, $add);
}
}
return $items;
}
public function main()
{
$scan_tree = $this->scandir_through($dir);
echo "<ul id='booklist'>"."</n>";
foreach ($scan_tree as $key=>$file){
$url = $file;
$xml = simplexml_load_file($url);
$book_count = count($xml->book);
for($i = 0; $i < $book_count; $i++) {
$book = $xml->book[$i];
$title=$xml->book[$i]->title;
$author=$xml->book[$i]->author;
//echo '</br>';
//echo $file. "   ";
//echo $title. "   ";
//echo $author;
echo "<li><div class='file'>".$file."</div>
<div class='title'>".$title."</div>
<div class='author'>".$author."</div></li></n>";
}
}
echo "</ul>";
}
}
$d = new data();
$d->main();
?>