I've got this code in PHP5
<?php
function debug_log($log) {
echo $log;
}
// Supporting classes
class Tag {
private $id;
private $name;
private $tagname;
private $extras;
public function __construct($tag) {
$this->id = 0; // Set to non-zero if you want an id
$this->name = ''; // Set to non-empty for a name
$this->tagname = $tag; // This is what tag, e.g. select/html/head
$this->extras = ''; // If we need anything extra that isn't covered
}
protected function OpenTag() {
debug_log('Tag::OpenTag()');
$html = '<' . $this->tagname;
if ($this->id != 0) {
$html .= 'id = ' . $this->id;
}
if ($this->name != '') {
$html .= 'name = ' . $this->name;
}
if ($this->extras != '') {
$html .= ' ' . $this->extra;
}
$html .= '>';
echo $html;
}
protected function CloseTag() {
debug_log('Tag::CloseTag()');
$html = '</' . $this->tagname . '>';
echo $html;
}
public function ID($new_id) {
$this->id = $new_id;
return $this;
}
public function Name($new_name) {
$this->name = $new_name;
return $this;
}
public function Extras($newextras) {
$this->extras = $newextras;
return $this;
}
public function Emit() {
Tag::OpenTag();
Tag::CloseTag();
}
}
// Unfortunately, totally necessary.
function MakeTag($arg) {
return new Tag($arg);
}
class ContentTag extends Tag {
private $tags;
public function __construct($tagname) {
parent::__construct($tagname);
$this->tags = array();
}
public function AddTag($tag) {
array_push($this->tags, $tag);
return $tag;
}
public function Emit() {
debug_log('In ContentTag::Emit()');
Tag::OpenTag();
debug_log('Emitting ' . count($this->tags) . ' tags.');
for($i = 0; $i < count($this->tags); $i++) {
if (is_a($this->tags[i], "Tag")) {
echo 'Emitting a tag - ';
$this->tags[i]->Emit();
} else {
echo 'Not emitting a tag of type '. get_class($this->tags[i]);
echo (string)$this->tags[i];
}
}
Tag::CloseTag();
}
}
function MakeContentTag($arg) {
return new ContentTag($arg);
}
class Listbox extends Tag {
private $options;
public function __construct() {
parent::__construct('select');
$this->options = array();
$this->name = '';
}
public function AddOption($name) {
array_push($this->options, $name);
}
public function SetName($new_name) {
$this->name = $new_name;
}
public function Emit() {
OpenTag();
for($i = 0; $i < sizeof($this->options); $i++) {
$innerHTML = $innerHTML . '<option>' . $options[i] . '</option>';
}
echo $innerHTML;
CloseTag();
}
}
class Title extends Tag {
private $title;
public function __construct() {
parent::__construct('title');
}
public function SetTitle($newtitle) {
$this->title = $newtitle;
}
public function Emit() {
OpenTag();
echo $this->title;
CloseTag();
}
}
class Link extends Tag {
private $href;
private $rel;
private $type;
private function AddExtras() {
Tag::Extras('href=' . $href . ' rel=' . $rel . ' type=' . $type);
}
public function __construct($ahref, $arel, $atype) {
parent::__construct('link');
$this->href = $ahref;
$this->rel = $arel;
$this->type = $atype;
$this->AddExtras();
}
public function Href() {
return $this->href;
}
public function Rel() {
return $this->rel;
}
public function Type() {
return $this->type;
}
public function SetType($atype) {
$this->type = $atype;
$this->AddExtras();
return $this;
}
public function SetHref($ahref) {
$this->href = $ahref;
$this->AddExtras();
return $this;
}
public function SetRel($arel) {
$this->rel = $arel;
$this->AddExtras();
return $this;
}
}
function MakeLink($a, $b, $c) {
return new Link($a, $b, $c);
}
class Img extends Tag {
public function __construct($src, $alt) {
parent::__construct('img');
Tag::Extras('src=' . $src . ' alt=' . $alt);
}
}
class DOM extends ContentTag {
private $body;
private $head;
public function __construct() {
parent::__construct('html');
Tag::Extras('xmlns="http://www.w3.org/1999/xhtml"');
$this->head = new ContentTag('head');
$this->body = new ContentTag('body');
$this->AddTag($this->head);
$this->AddTag($this->body);
$this->head->AddTag(MakeTag('meta')->Extras('http-equiv="Content-Type" content="text/html; charset=utf-8"'));
}
public function Head() {
return $this->head;
}
public function Body() {
return $this->head;
}
public function Emit() {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
ContentTag::Emit();
}
}
// top.php
$pagediv = 0;
$view = new DOM();
$view->Head()->AddTag(MakeLink('css/style.css', 'stylesheet', 'text/css'));
$view->Head()->AddTag(MakeLink('favicon.ico', 'icon', 'image/x-icon'));
$view->Head()->AddTag(MakeContentTag('div')->ID('top_grad'));
$div = $view->Body()->AddTag(MakeContentTag('div')->Extras('align="center"'));
$topdiv = $div->AddTag(MakeContentTag('div')->ID('top'));
$topdiv->AddTag(MakeContentTag('div')->ID('logo'))->AddTag(new Img('images/loughborough-logo.png', 'Loughborough University'));
$topdiv->AddTag(MakeContentTag('div')->ID('title'))->AddTag('TimeTable System');
$topdiv->AddTag(MakeContentTag('div')->ID('roundnumber')->Extras('align="right"'))->AddTag('Welcome ' . $_SESSION['username'] . '</br>' . 'Round Number: 1');
$page = $topdiv->AddTag(MakeContentTag('div')->ID('page'));
$view->Emit();
?>
It's quite a bit of code, but frankly, I'm just not sure what to post. The ContentTag::Emit method is failing. Specifically, variables inside $tags
are failing the instanceof Tag
check, even though they are of type ContentTag
, and get_class
confirms this. But if I remove the check and Emit anyway, then it says that Emit does not exist- even though I just confirmed using get_class
that those variables are of type ContentTag. The quantity is correct, so I'm pretty sure that all the preceeding code is correct too. I've also changed between is_a
and instanceof
and they both exhibit the same problems.
The only issue i have is with you forgetting your $
variable tokens on the increment value i
in your loops:
PHP Notice: Undefined index: i in dom.php on line 74
PHP Notice: Use of undefined constant i - assumed 'i' in dom.php on line 78
PHP Notice: Undefined index: i in dom.php on line 78
Not emitting a tag of type PHP Notice: Use of undefined constant i - assumed 'i' in dom.php on line 79
PHP Notice: Undefined index: i in /dom.php on line 79
PHP Notice: Use of undefined constant i - assumed 'i' dom.php on line 74
PHP Notice: Undefined index: i in dom.php on line 74
PHP Notice: Use of undefined constant i - assumed 'i' in dom.php on line 78
PHP Notice: Undefined index: i in dom.php on line 78
Not emitting a tag of type PHP Notice: Use of undefined constant i - assumed 'i' in dom.php on line 79
PHP Notice: Undefined index: i in dom.php on line 79