html没有以我期望的方式在网页源中打印出来


html not printing out the way I expect it to in the webpage source

有人能告诉我,在保持相同抽象级别的情况下,如何正确地做到这一点吗?我有这两门基础课,但并没有达到我想要的效果。最终结果应为:

<div class="test">
    content
</div>

如果我这样做:

class Wrapper{
    protected $_html = '';
    public function open(){
        $this->_html .= '<div class="test">';
    }
    public function close(){
        $this->_html .= '</div>';
    }
    public function __toString(){
        return $this->_html;
    }
}
class Content{
    protected $hmtl .= '';
    public function content(){
      $wrapper = new Wrapper();
      $wrapper->open();
      $this->html .= 'test';
      $wrapper->close();
    }
    public function __toString(){
        return $this->_html;
    }
}

$content=new content();echo$content->content();

我知道,是的,这是从来源:

"content";

如果我这样做:

class Wrapper{
    protected $_html = '';
    public function open(){
        echo $this->_html .= '<div class="test">';
    }
    public function close(){
        echo $this->_html .= '</div>';
    }
    // Technically don't need this
    public function __toString(){
        return $this->_html;
    }
}

我知道,是的,这是来自

<div class="test"></div>
"content"

那么我做错了什么?以及如何保持相同的抽象级别并获得所需的输出?

您想要某种方法将内容添加到包装器中,例如

public function addContent($content){
        $this->_html .= $content;
    }

你可以通过

$wrapper = new Wrapper();
$content = new Content();
$wrapper->open();
$wrapper->addContent((string)$content);
$Wrapper->Close();

您永远不会调用会产生HTML标记的Wrapper__toString方法。我想你真正想要的是$this->_html = (string)$wrapper->open()这样的东西。

编辑

事实上,我无法理解您的整个设置逻辑。为什么不使用->open()和->close()方法中的return呢?