【发布时间】:2011-10-12 09:45:18
【问题描述】:
我不明白为什么这是一个错误。一切对我来说都有意义。 $logFile 是 SimpleXMLElement 并且他们应该能够使用 getName() 方法。
错误:
致命错误:在第 29 行对 C:\xampp\htdocs\objLogParser.php 中的非对象调用成员函数 getName()
我已经在我的代码中标记了错误。
<?php
class objLogParser
{
private $fileName;
private $logFile;
//Constructor
public function __construct($varFileName)
{
$this->fileName = $varFileName;
//Load as string
$xmlstr = file_get_contents($varFileName);
$logFile = new SimpleXMLElement($xmlstr);
//Load as file
$logFile = new SimpleXMLElement($varFileName,null,true);
}
public function printNodes()
{
$this->printHelper($this->logFile,0);
}
public function printHelper($currentNode, $offset)
{
echo $this->offset($offset);
echo $currentNode->getName(); ////////////////////LINE 29 ERROR
if($currentNode->attributes()->count() > 0)
echo "({$currentNode->attributes()})";
echo " {$currentNode}";
foreach ($currentNode->children() as $child) {
echo "<br>";
printHelper($child, ($offset+1));
}
}
function offset($offset)
{
for ($i = 0; $i < $offset; $i++)
echo "_ ";
}
}
?>
【问题讨论】: