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内容{
保护$ hmtl。='';
public function content(){
$ wrapper = new Wrapper();
$ wrapper-> open();
$ this-> html。='test';
$ wrapper-> close();
}
public function __toString(){
return $ this-> _html;
$ / code $ / pre>
$ 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>';
}
//技术上不需要这个
公共函数__toString(){
return $ this-> _html;
}
}
我明白了,是的,
< div class =test>< / div>
content
那么我做错了什么?如何保持相同的抽象级别并获得所需的输出?
解决方案您需要某种方法来添加内容放入包装中,例如
public function addContent($ content){
$ this-> _html。= $内容;
}
您可以通过
$ wrapper = new Wrapper();
$ content = new Content();
$ wrapper-> open();
$ wrapper-> addContent((string)$ content);
$ Wrapper-> Close();
Can some one show me, keeping the same level of abstraction, how to do this properly? I have these two basic classes, that don't do as I want. The end resualt should be:
<div class="test">
content
</div>
If I do it this way:
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();
I get, and yes this is from the source:
"content";
If I do it this way:
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;
}
}
I get, and yes this is from the source,
<div class="test"></div>
"content"
So what am I doing wrong? and how do I keep the same level of abstraction and get the desired output?
解决方案 You want some kind of method to add content into the wrapper such as
public function addContent($content){
$this->_html .= $content;
}
and you can do this by
$wrapper = new Wrapper();
$content = new Content();
$wrapper->open();
$wrapper->addContent((string)$content);
$Wrapper->Close();
这篇关于html没有按照我期望的方式打印到网页源代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!