0

I have this Class

Class Parser extends DOMDocument {

    public function setHTML($file) {
        $this->loadHTMLFile($file);
    }

    public function setAttribute($name, $value) {
        return $this->setAttribute($name, $value);
    }

    public function findById($id) {
        return $this->getElementById($id);
    }

}

and I use it like this:

$parser = new Parser();
$parser->setHTML('as.html');
$parser->findById("xaxa")->setAttribute('name1', 'value1');

but if I have to see the changed HTML I call SAVEHTML like this

echo $parser->saveHTML();

Is there a way to make it automatic? Like when method setAttribute is called to make

$this->saveHTML() 

auto so I will have this

$html =$parser->findById("xaxa")->setAttribute('name1', 'value1'); 

then call

echo $html; 

Thanks alot

5
  • your function saveHTML() where is declared in wich class or file? Commented Nov 23, 2013 at 10:25
  • In DOMDocument Class ufc. Commented Nov 23, 2013 at 10:29
  • In your class you're already calling parent class's functions from with yours... Commented Nov 23, 2013 at 10:36
  • I know how it will work but i don't want it that way. Everytime i should make $html = $parser->saveHTML(); echo $html; and again and again i just want $parser->SaveHTML() to be called auto... thats all Commented Nov 23, 2013 at 10:47
  • if my solution fixes your problem, please accept it as as correct answer - so when others encounter your OP know how to solve the issue. Commented Nov 23, 2013 at 10:53

1 Answer 1

2

Well, the DOM object cannot be used (directly) as string, and when you'll try for example echo it will throw an exception

Catchable fatal error: Object of class DOMDocument could not be converted to string in ...

The saveHTML method is explicitly designed to return the nodes as HTML string - than you can echo it. Bear in mind, that actually the nodes are already updated (saved!) after you call the setAttribute method - the saveHTML is just for rendering a html string from the nodes.

Providing I understand your concept, and you still think you want it your way, maybe you can try the below solution - but just for the record, I didnt test the code.

Class Parser extends DOMDocument 
{

    public function setHTML($file) 
    {
    $this->loadHTMLFile($file);
    }

    public function setAttribute($name, $value) 
    {
        return $this->setAttribute($name, $value);
    }

    public function findById($id) 
    {
        return $this->getElementById($id);
    }

    public function __toString()
    {
        return $this->saveHTML(); 
    }
}

// and now this should work

$parser = new Parser();
$parser->setHTML('as.html');
$parser->findById("xaxa")->setAttribute('name1', 'value1');
echo $parser; 
Sign up to request clarification or add additional context in comments.

Comments

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.