How do I call superclass methods?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wvxvw
Forum Newbie
Posts: 22
Joined: Sat May 17, 2008 10:55 am

How do I call superclass methods?

Post by wvxvw »

Hi all.
I've red the manual, but may be didn't understood it... So, if anyone can tell me what I'm doing wrong would make me really happy =)
Code first:

Code: Select all

<?php
class Footer extends DOMDocument {
    var $body;
    
    function Footer ($ids, $names, $style) {
        $this->body = parent::appendChild(parent::createElement("div"));
        //parent->appendChild();
        //$this->appendChild();
        //DOMDocument::appendChild();
        //DOMDocument->appendChild();
        //these are wrong too...
        foreach ($ids as $id) {
            $button = $this->body->appendChild(parent::createElement("div"));
            $button->setAttribute("id", "footer_btn" . $id);
        }
        foreach ($this->childNodes as $button) {
            $button->setAttribute("class", $style);
        }
        $i = 0;
        foreach ($this->childNodes as $button) {
            $button->appendChild(parent::createTextNode($names[$i]));
            $i++;
        }
    }
    function toString () {
        $this->formatOutput = true;
        return  $this->saveXML();
    }
}
?>
I'm trying to create a class which I can later add to the whole document structure, so I want it to be a valid DomDocument/DomElement. I.e. I would be later able to do:

Code: Select all

$footer = new Footer($ids_array, $names_array, $style);
$another_document->appendChild($footer);
I'm stuck at calling superclass methods. The error I get sais:
Couldn't fetch Footer in <...> on line 6.
Please, help...

BTW. I tried to do it like this to... no luck yet =)

Code: Select all

<?php
class Footer extends DOMDocument {
    var $body;
    
    function __construct ($ids, $names, $style) {
        parent::__construct("1.0");
        $this->body = parent::appendChild(parent::createElement("div"));
        foreach ($ids as $id) {
            $button = $this->body->appendChild(parent::createElement("div"));
            $button->setAttribute("id", "footer_btn" . $id);
        }
        foreach ($this->childNodes as $button) {
            $button->setAttribute("class", $style);
        }
        $i = 0;
        foreach ($this->childNodes as $button) {
            $button->appendChild(parent::createTextNode($names[$i]));
            $i++;
        }
    }
 
    function toString () {
        $this->formatOutput = true;
        return  $this->saveXML();
    }
}
?>
Ok, never mind, finaly sorted it out. The problem was that DomDocument can't appendChild() if it was created by another instance of DomDocument (kind of useless feature...). So, I had to call ->importNode([link to the node from another DomDocument], true); and only than I could addChild() it.

The result code:
(in case someone may need it)

Code: Select all

<?php
include_once("footer.class");
class Template {
    var $dtd = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
    var $all;
    var $html;
    var $head;
    var $body;
    var $footer;
    
    function Template($type){
        $this->all = new DomDocument("1.0");
        $this->html = $this->all->appendChild($this->all->createElement("html"));
        $this->head = $this->html->appendChild($this->all->createElement("head"));
        $this->body = $this->html->appendChild($this->all->createElement("body"));
        $title = $this->head->appendChild($this->all->createElement("title"));
        $title->appendChild($this->all->createTextNode($type));
        $meta = $this->head->appendChild($this->all->createElement("meta"));
        $meta->setAttribute("http-equiv", "Content-Type");
        $meta->setAttribute("content", "text/html; charset=utf-8");
        $style = $this->head->appendChild($this->all->createElement("link"));
        $style->setAttribute("rel", "StyleSheet");
        $style->setAttribute("href", "site_style.css");
        $style->setAttribute("type", "text/css");
        $ids = array(1, 2, 3, 4);
        $names = array("home", "gallery", "contact", "about");
        $style = "btn";
        $this->footer = new Footer($ids, $names, $style);
        $this->body->appendChild($this->all->createCDATASection($this->footer->toString()));
        $footer_node = $this->all->importNode($this->footer->body->cloneNode(true), true);
        $this->body->appendChild($footer_node);
    }
    function toString() {
        $this->all->formatOutput = true;
        $out = $this->all->saveXML();
        $out = preg_replace("/^<\?xml[^>]+>/", $this->dtd, $out);
        return $out;
    }
}
?>

Code: Select all

<?php
class Footer extends DOMDocument {
    public $body;
    
    function __construct ($ids, $names, $style) {
        parent::__construct("1.0");
        $this->body = $this->appendChild($this->createElement("div"));
        foreach ($ids as $id) {
            $button = $this->body->appendChild($this->createElement("div"));
            $button->setAttribute("id", "footer_btn" . $id);
        }
        $i = 0;
        foreach ($this->body->childNodes as $button) {
            $button->setAttribute("class", $style);
            $button->appendChild($this->createTextNode($names[$i]));
            $i++;
        }
    }
 
    function toString () {
        $this->formatOutput = true;
        $out = $this->saveXML();
        $out = preg_replace("/<\?xml[^>]+>/", "", $out);
        return  $out;
    }
}
?>
Post Reply