How do I call superclass methods?
Posted: Sat Jun 21, 2008 5:55 am
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:
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:
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 =)
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)
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();
}
}
?>Code: Select all
$footer = new Footer($ids_array, $names_array, $style);
$another_document->appendChild($footer);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();
}
}
?>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;
}
}
?>