Dit is cel A1 |
Dit is cel A2 |
Dit is cel A3 |
<?php
class Table {
private $_rows;
public function __construct() {
$this->_rows = array();
}
public function append($row) {
$this->_rows[] = $row;
}
public function draw() {
echo '<table border="1">'.PHP_EOL;
foreach($this->_rows as $row) {
$row->draw();
}
echo '</table>'.PHP_EOL;
}
}
class Row {
private $_cells;
public function __construct() {
$this->_cells = array();
}
public function append($cell) {
$this->_cells[] = $cell;
}
public function draw() {
echo '<tr>'.PHP_EOL;
foreach($this->_cells as $cell) {
$cell->draw();
}
echo '</tr>'.PHP_EOL;
}
}
class Cell {
protected $_content;
public function __construct($content) {
$this->_content = $content;
}
public function draw() {
echo '<td>'.$this->_content.'</td>'.PHP_EOL;
}
}
class Strong_Cell extends Cell {
public function __construct($content) {
parent::__construct($content);
}
public function draw() {
echo '<td><strong>'.$this->_content.'</strong></td>'.PHP_EOL;
}
}
/* Procedurele code */
$cellA1 = new Cell('Dit is cel A1');
$cellA2 = new Strong_Cell('Dit is cel A2');
$rowA = new Row();
$rowA->append($cellA1);
$rowA->append($cellA2);
$rowA->append(new Cell('Dit is cel A3')); // Zo kan het ook!
$table = new Table();
$table->append($rowA);
$table->draw();
show_source(__FILE__);
?>