PHP5- instanceof / is_a失败?

时间:2011-02-23 18:21:58

标签: php types

我在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();
?>

这是相当多的代码,但坦率地说,我只是不确定要发布什么。 ContentTag :: Emit方法失败。具体来说,$tags内的变量未通过instanceof Tag检查,即使它们属于ContentTag类型,get_class也确认了这一点。但是如果我无论如何都删除了支票和Emit,那么它说Emit不存在 - 即使我刚刚确认使用get_class这些变量属于ContentTag类型。 数量是正确的,所以我很确定所有前面的代码也是正确的。我也在is_ainstanceof之间进行了更改,他们都表现出同样的问题。

1 个答案:

答案 0 :(得分:1)

我遇到的唯一问题是您在循环中忘记了$变量标记的增量值i

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