I can't figure out why this is an error. Everything makes sense to me. $logFile
is a SimpleXMLElement
and theirfore should be able to use the getName() method.
Error:
Fatal error: Call to a member function getName() on a non-object in C:\xampp\htdocs\objLogParser.php on line 29
I've marked the error in my code.
<?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 "开发者_StackOverflow<br>";
printHelper($child, ($offset+1));
}
}
function offset($offset)
{
for ($i = 0; $i < $offset; $i++)
echo "_ ";
}
}
?>
It should be $this->logFile->getName();
. Essentially, replace $logFile
with logFile
.
$this->logFile is never being set. $logFile is set in __construct(), but is never actually used. $this->logFile and $logFile aren't the same variable. $logFile is limited to the scope of __construct(); you can't access it outside that method. $this->logFile can be accessed by any method in the class. Try changing $logFile in __construct() to $this->logFile.
精彩评论