Nie wiem do czego Ci taka funkcjonalność potrzebna, jak do testowania obiektowości w php, to ok, jak do generowania kodu html, to moim zdaniem przerost formy nad treścią. Co do pytania, to nie wiadomo jak później chcesz te obiekty wykorzystywać, możesz je przechowywać w formie tablicy lub wykorzystać jakąś klasę z SPL (np. stos lub kolejkę)
Przykład z wykorzystaniem tablicy:
class Tag {
protected $name = '', $attributes = '', $closed = true, $children = array();
function __construct($name, $closed = true) {
$this->name = $name;
$this->closed = $closed;
}
function add($item) {
$this->children[] = $item;
return $this;
}
function addAttributes($item) {
$this->attributes .= ' ' . $item;
return $this;
}
function render() {
$returns = '<' . $this->name . $this->attributes . '>';
foreach ($this->children as $v) {
$returns .= ($v instanceof Tag) ? $v->render() : $v;
}
if ($this->closed) {
$returns .= '</' . $this->name . '>';
}
return $returns;
}
function __toString() {
return $this->render();
}
}
Wykorzystanie: przykład 1, przykład 2