sID = $id; if (is_array($attributes)) { $this->aAttributes = $attributes; } if (is_array($contents)) $this->aContents = $contents; else $this->add($contents); } function __get($nm) { if (isset($this->aAttributes[$nm])) { $r = $this->aAttributes[$nm]; return $r; } } function __set($nm, $val) { // if (isset($this->aAttributes[$nm])) { $this->aAttributes[$nm] = $val; // } } function __isset($nm) { return isset($this->aAttributes[$nm]); } function __unset($nm) { unset($this->aAttributes[$nm]); } function add($obj) { $this->aContents[] = $obj; } function set($obj) { $this->aContents = array($obj); } function get_id() { return $this->sID; } function has_id() { return !is_null($this->sID); } function has_attributes() { return count($this->aAttributes); } function get_attributes() { return $this->aAttributes; } function get_contents() { return $this->aContents; } protected function paint_head() { if ($this->has_id()) { if ($this->has_attributes()) { return sprintf("<%s %s>", strtolower($this->get_id()), $this->paint_attributes()); } else { return sprintf("<%s>", strtolower($this->get_id())); } } } protected function paint_attributes() { $r = array(); foreach($this->get_attributes() as $k=>$v) { $k = strtolower($k); if (is_string($v)) { $r[] = "$k=\"$v\""; } elseif (is_object($v)) { $r[] = "$k=\"".$v->__toString()."\""; } } return implode(' ',$r); } protected function paint_foot() { if ($this->has_id()) return sprintf("", strtolower($this->get_id())); } protected function paint_contents($split='') { $r = array(); foreach($this->get_contents() as $item) { if (is_string($item) && is_callable($item)) { $args = func_get_args(); $r[] = ''.call_user_func($item,$args); } elseif (is_object($item)) { $r[] = ''.$item->__toString(); } elseif (is_string($item)) { $r[] = ''.$item; } } return implode($split,$r); } function __toString() { return $this->paint_head().$this->paint_contents().$this->paint_foot(); } } class Entity extends Container {}