0

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 "<br>";
            printHelper($child, ($offset+1));
        }
    }

    function offset($offset)
    {
        for ($i = 0; $i < $offset; $i++)
            echo "_ ";
    }



}
?>

2 Answers 2

1

It should be $this->logFile->getName();. Essentially, replace $logFile with logFile.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the post. Made the change, updated error, still remains.
The new error message is saying that $currentNode is not an object. Judging by the code that you gave, I assume that you meant to put $this->logFile instead of $logFile in your __construct function.
1

$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.

1 Comment

Yes that did it, I also had some more similar issues without the $this in front of it. Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.