element_name = $element_name;
$this->attributes = array();
$this->child_elements = array();
$this->is_empty = $is_empty;
}
function set_attribute($name, $value)
{
$this->attributes[$name] = $value;
}
function get_attribute($name)
{
return $this->attributes[$name];
}
function add_element($element)
{
if ($this->is_empty)
{
die("Error: the HTMLElement " . get_class($this) . " is empty and thus does not allow adding of elements.");
}
else
{
$this->child_elements[] = $element;
}
}
function to_html()
{
$result = "";
// create attribute name="value" pairs
$attributes = " ";
foreach ($this->attributes as $attribute => $value)
{
if ($value == "")
{
$attributes .= $attribute . " " ;
}
else
{
$attributes .= $attribute . "=\"" . htmlentities($value, ENT_COMPAT) . "\" ";
}
}
// create start tag
$result = "<" . $this->element_name . rtrim($attributes);
// end start tag the xhtml way
if ($is_empty)
{
$result .= "/>";
}
else // include children elements followed by an end tag
{
$result .= ">";
// create html between start and end tags
$inner_html = "";
foreach($this->child_elements as $element)
{
if (is_subclass_of($element, "HTMLElement") || get_class($element) == "htmlelement")
{
$inner_html .= $element->to_html();
}
else
{
$inner_html .= $element;
}
}
$result .= $inner_html . "" . $this->element_name . ">\n";
}
return $result;
}
}
?>