children = $args; return $this->__toString(); } $this->attributes = $args; return $this; } public function __toString(): string { $rendered = '<' . $this->name; foreach ($this->attributes as $attribute => $value) { $rendered .= ' ' . $attribute; if ($value !== true) { $rendered .= '="' . htmlentities($value) . '"'; } } $rendered .= '>'; if ($this->children) { $rendered .= implode('', $this->children) . 'name . '>'; } return $rendered; } } $element = fn(string $name) => new Element($name); $table = $element('table'); $th = $element('th'); $tr = $element('tr'); $td = $element('td'); echo $table(id: 'table-id')( $tr( $th('Description'), $th(class: 'text-right')('Price'), ), ...array_map( fn(array $row) => $tr( $td($row['description']), $td(number_format($row['price'], 2)), ), [ [ 'description' => 'Foo', 'price' => 1.99, ], [ 'description' => 'Bar', 'price' => 2.88, ], [ 'description' => 'Baz', 'price' => 3.77, ], [ 'description' => 'Bay', 'price' => 4.66, ], ], ), );