【发布时间】:2012-11-09 09:42:11
【问题描述】:
我希望有人可以帮助我,我是 oop 初学者,下面的脚本给出了输出 < 但我想要这个 <input type="text">。
现在我创建了一个 Build 类,但它不起作用。我做错了什么,我该如何解决这个问题?
有人可以帮助我吗?
谢谢!
//set get Element
class Element {
private $_element;
function addElement($element) {
$this->_element = $element;
}
function getElement(){
return $this->_element;
}
}
//add and set Atrr
class attrType extends Element {
//set var
public $_attrType;
function __construct(){
$this->_attrType = array();
}
function addAttr($attrType, $attrValue){
$this->_attrType[$attrType] = $attrValue;
}
function getAttr(){
return $this->_attrType;
}
}
//build input text field
class Build extends attrType {
function Builder() {
$html .= "<";
$html .= ''.$this->getElement();
foreach( $this->getAttr() as $key => $value){
$html .= " $key=";
$html .= "\"$value\">";
}
return $html;
}
}
$element = new Element();
$attr = new attrType();
$build = new Build();
$attr->addElement('input');
$attr->addAttr('type','text');
echo $build->Builder();
【问题讨论】:
-
“它不起作用”从来都不是一个好的问题描述。发生了什么或没有发生什么?
-
您在一个对象上调用
addElement和addAttr并在另一个对象上调用Builder。 -
问题是它显示 所以构建器类不起作用,this->getElement() 和 $this->getAttr()
标签: php input-field